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:
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
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.
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.