Question by
Msenol · Aug 24, 2020 at 10:59 AM ·
unity 2dtimecolor change
How to change sprite color with time ?
Hello,
i have player sprite. it is just a white circle.
I want to change minimum 5 different colors with time.
For example;
every 2 seconds sprite must change its color.
How can i do that?
i found this solution but when i start game, color is fading and sprite is disappearing smoothly and than it stopped.
SpriteRenderer cubeMeshRenderer; [SerializeField] [Range(0f, 1f)] float lerpTime;
[SerializeField] Color[] myColors;
int colorIndex = 0;
float t = 0f;
int len;
// Start is called before the first frame update
void Start()
{
cubeMeshRenderer = GetComponent<SpriteRenderer>();
len = myColors.Length;
}
// Update is called once per frame
void Update()
{
cubeMeshRenderer.material.color = Color.Lerp(cubeMeshRenderer.material.color, myColors[colorIndex], lerpTime*Time.deltaTime);
t = Mathf.Lerp(t, 1f, lerpTime*Time.deltaTime);
if (t > .9f)
{
t = 0f;
colorIndex++;
colorIndex = (colorIndex >= myColors.Length) ? 0 : colorIndex;
}
}
Comment
Answer by xxmariofer · Aug 24, 2020 at 12:08 PM
probably all the colors in your array have the alpha set to 0
Your answer
