I don’t want to color each gameobject in the array in 5 seconds.
I want to color all the gameobjects in the array in 5 seconds.
This is the Colors script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Colors : MonoBehaviour
{
public GameObject[] objectsToColor;
public Color startColor;
public Color endColor;
public float colorDuration;
public void Initis()
{
foreach (GameObject rend in objectsToColor)
{
startColor = rend.GetComponent<Renderer>().material.color;
}
endColor = Color.green;
}
public IEnumerator ChangeColor()
{
float t = 0;
while (t < colorDuration)
{
foreach (GameObject rend in objectsToColor)
{
t += Time.deltaTime;
rend.GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}
}
}
And this is how I’m using it:
if (Input.GetKeyDown(KeyCode.C))
{
StartCoroutine(colors.ChangeColor());
}
The problem is that it’s coloring all the gameobjects in the array very fast at once.
And i set in the Inspector the colorDuration value to 5. But it’s not coloring the gameobjects in 5 seconds but coloring them at once in one frame.
Again i want to color all the gameobjects in the array at once but over 5 seconds !
Can’t figure out how to do it.