- Home /
Question by
alexanderameye · Mar 20, 2017 at 01:57 PM ·
gameobjectfunctionnullvoidgameobject.find
Check if GameObject.Find returns null
When I use GameObject.Find
, could I, in a script, check if that functions returns void?
So in other words can I create an if statement that checks if a certain gameobject is not found in the hierarchy?
Thanks
Alex
Comment
Best Answer
Answer by ShadyProductions · Mar 20, 2017 at 02:11 PM
Ofcourse,
This function only returns active GameObjects. If no GameObject with name can be found, null is returned.
var item = GameObject.Find("test");
if (item == null)
//do whatever
For performance reasons, it is recommended to not use this function every frame. Instead, cache the result in a member variable at startup. or use GameObject.FindWithTag.
Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.
Awesome! Especially that information about performance is a great help :D