Different timer in a list of gameobject

Hello !

I have a list that contains GameObject. This list is empty at it start. At each collision I add the gameObject collisionned into the list. This parts works pretty well.

What I want to add to this is : Everytime I add a new gameObject to my list, there is a timer attached to this, and when the timer is done the gameObject is removed from the list.

I can’t do a global timer because every gameObject has to been handle separatly.

There is the code I already have (about adding gameObject into list) :

        if (!GM.collisionTab.Contains(collision.gameObject))
        {
            GM.collisionTab.Add(collision.gameObject);
            GM.compteurCollision++;
        }

and later on the code I use it to change material, but it doesn’t help the problem I have.

I have been thinking about doing an array/list of timers but i can’t update each timer at the same time and separatly.

Also, I’ve been thinking about using coroutine but again, i’m not sure it would solve the problem.

The last thing I’ve been thinking is doing a class that contains a gameObject and a timer, with a method that would update the timer, but since i’m pretty new in this i’m not really sure I could handle this myself.

Thank you for your help ! :slight_smile:

Hi @Dreaa !

You can use a coroutine, which you start each time you add a gameobject in your list :

         if (!GM.collisionTab.Contains(collision.gameObject))
         {
             GameObject go = collision.gameObject;
             GM.collisionTab.Add(go);
             GM.compteurCollision++;
             StartCoroutine(ChangeColor(go));
         }

        IEnumerator ChangeColor(GameObject go)
    	{
             print("ENTER : " + go.name);
             yield return new WaitForSeconds(2);
             print("EXIT : " + go.name);
             go.GetComponent<Renderer>().sharedMaterial.color = Color.white;
    	}

@Staarter Hello, and thank you !

I wasn’t expecting Coroutine to work because new gO are added faster than the old one are deleted from the list. So I was thinking Coroutine wouldn’t be able to work since it will have to do something like instantiate multiple Coroutine at the same time.

But apparently it works like a charm, and I thank you very much :slight_smile: