Which is more efficient GameObject.Find() or public GameObject??
There are two approaches of getting access to a game object in a script: 1) GameObject.Find() 2) public GameObject object (and then drag the game object in the inspector)
Can anyone tell me which is more efficient? I am working on a game and i need to turn of lots of gameObjects to optimize the performance. I know that i can do it both ways but which one will be more efficient performance wise ??
I did find one question but i am looking for a more detailed answer :)
Answer by Immanuel-Scholz · Apr 13, 2016 at 01:35 PM
Assigning in the inspector is by far more efficient in terms of speed.
GameObject.Find loops through all existing GameObjects and compares their names. Persisted direct links are basically free and initialized when the scene is loaded.
Said that, direct assignments have a couple of draw-backs:
You need to remember to assign them, which can become quite a burden depending on your workflow.
You can only reference objects that are already present in the editor. So e.g. if you dynamically create objects from script code later, these can not be assigned in the editor (obviously).
The pointer use up (very few) memory in the component you need them. This is usually of no real concern, but if you plan to have a LOT of links which you need only once at a blue moon, it might be better to only search them at the time you need them.
If you reference any Asset (Texture, Mesh etc) or any prefab that contains an asset, this will be loaded immediately when the scene is loaded. (Of course, GameObject.Find() is not an alternative to direct links here. You need Resources.Load instead and place the assets into Resources/ folder. Just saying.. ;) )
So what should i do in my particular case? I enter a trigger, i close the door behind me and disable all the objects that are behind that door. The script is on the trigger !!
I do agree with your answer and thats somewhat i found and thought previously but how should we find which one is the right thing to do?
Lets say i used the public variable method on my script (which i did), this loads the gameobjects to be disabled when the game begins in the script (which is not used at the moment). On the other hand, if i use Find() on that particular moment then it will loop through every game object finding the right ones to be disabled. So the point is, do i load the gameobjects at the start of the gameplay effeciently (which will be used at some point later in the game) or i load the gameobjects inefficiently when i actually need them ? (i hope i make sense)
If you are not sure what method to use, then use any method until you got a reason to do otherwise.
Really, you are not here to philosophize about code, you are here to make games. ;)
If your choosen method has problems, (e.g. for GameObject.Find you experience stutters in gameplay when opening the door. Or for direct assignments you realize that would mean you have to drag'n'drop 3000 objects across 25 scenes to setup everything right).. then just switch to the other method.
If you still can't decide, you should use... dice rolling public field assignment. :P
Hahah or toss a coin. If the player cant run a game they should upgrade their machines haha. Thanks for your answer :)
Answer by maaboo · Apr 13, 2016 at 05:06 PM
The more efficient is GameObject.FindWithTag()
That is true. Unity caches GameObjects based on their tags, so in a big scene with lots of objects, GameObject.FindWithTag is several orders of magnitudes faster that GameObject.Find.
It still does not beat the simple deserialization of a direct public variable link (in terms of speed).
Just for the record: Object.FindObjectOfType is A LOT slower than GameObject.Find. In our measurements, the relation is roughly: 10x GameObject.FindWithTag = GameObject.Find and 100x GameObject.Find = Object.FindObjectOfType.
Of course, all that only matters if it really matters. 99% of the cases, it just does not matter so if you don't write a tight Update() function in a big scene targeting iPhone 3, go on with any method you like until you run into trouble.
Answer by SUfIaNAHMAD_ · Jan 03, 2021 at 04:47 AM
GameObject.Find is great when you have a small scene. GameObject.FindWithTag is better for larger scenes with 100s of gameObjects.
public static Object FindObjectOfType(Type type); is useful somewhere, when you have a small scene and you want to find which object has that specific Type. It helps in refactoring other codes.