Setting sprite transparency via CanvasItem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nullsalad

I’m trying to hide a sprite by changing its transparency. I see that a Sprite’s CanvasItem component has a Color() function, and that the last value controls alpha, so I’m trying to do something like:


	var canItem = GetNode("CanvasItem")

	canItem.Color(1,1,1,0.5)

This doesn’t work. How do I access the CanvasItem?

:bust_in_silhouette: Reply From: kidscancode

You can see the available properties and methods of CanvasItem here:

As you can see “Color” is not a property.

If you want to use transparency as in your example, you need to set the modulate property:

canItem.modulate = Color(1, 1, 1, 0.5).

If you’re just interested in transparency you can do canItem.modulate.a = 0.5.

There’s also a self_modulate counterpart if you don’t want the same effect applied to all of that Node’s children.

Hammer Bro. | 2018-09-07 21:32

:bust_in_silhouette: Reply From: OnaniMouse

see this https://www.youtube.com/watch?v=LAIaqZl_7c0

How to acces self_modulate?
I got this and it doesnt work:
$Sprite2D/CanvasModulate.self_modulate.a -= 1