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?
Answer by ShadyProductions
Mar 20 at 02:11 PM
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)
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
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
86 People are following this question.
Object reference not set to an instance of an object?
ID not recognized in function
Null reference when calling a method from another script
[UNSOLVED] Object reference not set to an instance of an object
DontDestroyOnLoad does not work after reload the scene?