How can I color array of gameobjects over time ?

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.

Hello @Chocolade !
From your explanation, i assume that all the gameobject’s color in the objectsToColor are the same. you could try like this in the Initis() :

public void Initis()
    {
        startColor = objectsToColor[0].GetComponent<Renderer>().material.color; //just get one color

        endColor = Color.green;
    }

and in the ChangeColor coroutine :

 public IEnumerator ChangeColor()
    {
        float t = 0;

        while (t < colorDuration)
        {
            t += Time.deltaTime;
            rend.GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t);
            yield return null;
        }
    }

//push the spacebar !!!

	public GameObject[] objectstocolor;
	public Color[] startcol;
	public Color endcol=Color.blue;
	public int seconds=5;

	Color diff;

	public float timer;
	public float amount;
	bool go;
	int i;

	void Start(){

		i = objectstocolor.Length;
		startcol = new Color*;*
  •  while (i>0) {i--;*
    

startcol_=objectstocolor*.transform.renderer.material.color;
}*_

* }*

* void Update () {*
* if(Input.GetKeyDown(“space”)){go=true;*

* }*
* if (go) {*
* i=objectstocolor.Length;*
* while(i>0){i–;*
_ diff = startcol - endcol;
* timer += Time.deltaTime;
amount = timer / seconds;
if(amount>=1){go = false;amount = 1;}
objectstocolor.renderer.material.color=startcol-(diffamount);

* }*_

* }*
* }*
you need to remember the start color for all the objects independently
then get the difference between each object’s start color and the endcolor
the timer creates a float between 0 and 1 using the amount seconds you want and time passed.
now this float can be multiplied to the difference then added to the startcolor to create the magic you are looking for.