Timer and SetActivate.

i’m trying to deactivate my gameobject once the timer reaches 0, but I want to be able to re-instantiate a new one with a button.

I got three gameobjects.
One gameobject with a timer.
second gameobject I want to instantiate/clone.
third gameobject with the spawning/instantiate script.

Timer on the first gameobject:

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour
{
    //default time value
    public static float Timer = 20f;


    // Update is called once per frame
    void Update()
    {
        //decrease time
        Timer -= Time.deltaTime;
        if (Timer < 0)
        {
            Debug.Log("End");
            //destroy the gameObject when time is up
            //Destroy(this);
            GameObject message1 = GameObject.FindGameObjectWithTag("message1");
            message1.SetActive(false);
        }
    }
}

And inside the other script (instantiate with a button) i wrote something like that:

GameObject message1 = GameObject.FindGameObjectWithTag("message1");
            message1.SetActive(true);

The problem is, it does not reactivate the gameobject with tag “message1”. I’m guessing it’s because message1.SetActive(false) is in “update”… But I don’t know where to go from there :frowning:

Any help is welcome!

You should stop using Find methods. They don’t find any inactive objects, instead store reference to GameObject somewhere and access it.

You mean I just should do like:

public GameObject message1;

and plug the message1 through the Unity editor?

That’s one way of doing it, yes.

Thank you!

I tried but it does not deactivate the clones but only the original object…? That’s why i used tags.

It doesn’t because you’re not referencing clones. You might consider keeping a List and each time you instantiate a clone you add it to that list. When you need to deactivate all, you just loop through list and deactivate each one. If all objects you need are initialy enabled, then at the very beginning you can add all of them you Find (by tag/name/whatever, doesn’t matter) into that list and it’ll contain all instances you need.

mmmhh thank you I will try that.

How do you you loop through list to deactivate one specific clone…?

You need some parameter you look for. For example, name:

//If declaration in class looks like
List<GameObject> list;

//Then inside method finding specific object is:
for(int a=0;a<list.Count;a++)
{
    if(list[a].name == "Specific name")
    {
        //Do something
        break; //Needed not to continue looping through elements you know you won't need
    }
}

It can be position, rotation, well, anything as long as it’s variable and is comparable.

Oh i see, thank you very much