(Solved) Findout if an object exist in the scene, but dont trow exception
Hi! my question is simple
i want to check if an object in the scene exist, i know how to do that, but, from that, if the object is not found, i want to set a variable to false, but unity of course will trow a null reference exception and my code wont run, is there a way to tell if an object is not present wihtout unity trowing exceptions? this is for editor window scripting btw.
here is the code i have until now
private void CheckIfPrefabHierarchyExist()
{
Transform holder = GameObject.Find("Bacterias").transform;
if (holder == null)
{
hierarchyPrefabDetected = false;
}
else
{
hierarchyPrefabDetected = true;
}
}
Answer by BackslashOllie · Dec 10, 2015 at 05:16 PM
you are getting a null reference exception because your code is trying to return the transform of an object which doesn't exist. Try this:
private void CheckIfPrefabHierarchyExist()
{
if (GameObject.Find("Bacterias") == null)
{
hierarchyPrefabDetected = false;
}
else
{
hierarchyPrefabDetected = true;
}
}
thanks a lot, that seems like it would work, let me try it :) and report results.
Your answer
Follow this Question
Related Questions
setting a variable to a GameObject that has a certain RGB value??? please help 0 Answers
Why does GameObject.Find() work in ones and not in others? 1 Answer
GameObject.Find throws be a error when trying to find a child. 1 Answer
How Can I assign a gameobject via script? 0 Answers
How can we be notified the moment an object is instanced in the scene ? 3 Answers