I'm trying to animate the color of gameObjects that are referenced in array. like this
public GameObject[] laneMat;
void Update()
{
StartCoroutine(CountDownMat(laneMat, .3f));
}
IEnumerator CountDownMat(GameObject[] laneMat, float delay)
{
time += Time.deltaTime;
for(float i = 0; i < laneMat.Length; i++)
{
if (duration > time)
{
laneMat[i].GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, time / duration);
yield return new WaitForSeconds(delay);
}
}
}
What I'm Looking for is, to have the game objects change color in a sequence, between each of the objects there is a delay of .3 seconds.
However I keep getting Error: Cannot implicitly convert type float to int because of this line laneMat[i].GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, time / duration);
I'm having difficulty understanding the error because the array is type of gameObject so not really sure what is the convertion float to int here..
I tried another way of doing this by changing the for loop with foreach but did not work as intended.
Any help would be greatly appreciated
You declare the index (i) in your loop as float. Then you pass it as float into the indexer of the array. Which is what causes the error.
Just use an int instead and no type conversion is needed anymore.
for(int i = 0; i < laneMat.Length; i++)
{
if (duration > time)
{
laneMat[i].GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, time / duration);
yield return new WaitForSeconds(delay);
}
}