checking if gameobject exists
Hi. I use this code to deactivate gameobjects.
GameObject[] rigid;
void Start()
rigid = GameObject.FindGameObjectsWithTag("klocek");
foreach(GameObject rigids in rigid)
{
if(rigids.transform.position.x>30)
rigids.SetActive(false);
}
}
And then I need to activate them somewhen
void Update(){
foreach(GameObject rigids in rigid)
{
if(rigids.transform.position.x<p1)// if its time to activate you
rigids.SetActive(true);
}
}
Everything works untill some of objects get Destroyed. Then I take error: Trying to access gameobject than doesnt exist. Is there a way to avoid this?
Answer by Wolfshadow · Aug 23, 2015 at 01:14 PM
Well, instead of destroying them, you could move them of the scene with transform.Translate. Than it would always exist.
Your answer
Follow this Question
Related Questions
GameObject[] doesn't have a definition for transform/name/etc... 1 Answer
c# Getting Transforms from array made from FindGameObjectsWithTag 1 Answer
How to return a value from an array of GameObjects? 1 Answer
Check if 2 or more objects of same type is near object. 1 Answer
How would I access a property of another objects child component? 2 Answers