Improve alpha values ​​in the unit?

advertisements

I am making a game for Android using Unity 5.4.0f3 and am having trouble fading out a sprite's color.a value. I have tried animations, but they don't seem to work for me. I have tried using the code from here and here, but those didn't work either. I want the sprite to fade out over about 1 second when it is tapped. I think I need to use a for loop and Mathf.Lerp. Please help.

Edit: Here is an image of the Inspector view of the object I want to fade out.


Because Sprite-Default material is NOT modifiable.

Try this instead:

Easiest way:

/* Modify SpriteRenderer.color instead of Material.color */
//Get the renderer via GetComponent or have it cached previously
var renderer = GetComponent<SpriteRenderer>();
renderer.color = new Color(0f, 0f, 0f, 0f); // Do your lerping stuff with this property

If you still want to modify Material

  1. Create a new material in Project tab and name it whatever you want. Project tab -> Create -> Material.

  2. Assign it a shader like in the below image.

  3. Assign the newly created material to your Sprite Renderer Material slot. Notice that now you can modify material color from the inspector. It has not been grayed out any more. You should run you code again and it should work.