Reputation: 1664
I want to change the alpha value (transparency) of a color back and forth:
private void Awake()
{
material = gameObject.GetComponent<Renderer>().material;
oColor = material.color; // the alpha here is 0
nColor = material.color;
nColor.a = 255f;
}
private void Update()
{
material.color = Color.Lerp(oColor, nColor, Mathf.PingPong(Time.time, 1));
}
This doesn't work, the color instantly changes to white, and it keeps blinking the original color. I'm following this.
Upvotes: 3
Views: 9115
Reputation: 1815
In fact the alpha
color of the Material
goes from 0 to 255, however these values in C#
are translated from 0 to 1, making 1 equal to 255. This is a script I made a while ago, maybe you can use it:
public class Blink : MonoBehaviour
{
public Material m;
private Color c;
private float a = .5f;
private bool goingUp;
private void Start()
{
c = m.color;
goingUp = true;
}
private void Update()
{
c.a = goingUp ? .03f + c.a : c.a - .03f;
if (c.a >= 1f)
goingUp = false;
else if (c.a <= 00)
goingUp = true;
m.color = c;
}
}
Upvotes: 1
Reputation:
You can try this:
void FixedUpdate ()
{
material.color = new Color (0f, 0.5f, 1f, Mathf.PingPong(Time.time, 1));
}
Here the color is blue, (RGB 0, 128, 255) the "Mathf.PingPong(Time.time, 1) handles the alpha.
The effect is fully visible when you set the material "rendering mode" to "transparent" in the inspector :)
Note:
using the "transparent" rendering mode will make the object transparent when the alpha layer goes down to 0,
using the "fade" rendering mode will make the object become completely invisible when the alpha layer goes down to 0.
Upvotes: 0