GameObjects with identical names

Hello there.

To my surprise, Unity allows me to create multiple GameObjects with identical names. Isn’t this a clear drawback? For example, what if you are using the GameObject.Find() method, but you accidentally named two or more GO’s the same? This is not a problem for small games, but I still would like Unity to warn me (at least) when I have two GO’s with the same name. Is this possible? Thanks!

In big games, it’s very common to have objects with the same name.

Consider this: you have 20 enemies each firing machine guns, so there are 100s of bullets in the scene. It would be a bit silly to generate a unique name for each bullet.

GameObject.Find() is a convenience method for beginners, but it’s very inefficient because you’re basically looping through every object and checking if the name is what you’re looking for. It’s far better to use public fields and serialized fields to set up references to other objects so you don’t need to call a “Find” method.

The following manual page shows you that you can make public variables in code and fill out the values in the unity inspector. You can do the same thing with a public GameObject or other script you’ve made, just click-and-drag the other object from the Hierarchy into the empty field in the inspector.

3 Likes

Thank you Gambit-MSplitz for your useful answer. I will avoid the Find method at all costs now!

The Find method can be useful in Start or Awake functions to set stuff up (also FindObjectsWithTag is very useful and much more efficient), but it’s not something you want to use continuously, like in Update.

2 Likes

I’ll have that into account ProtoTerminator, thank you :wink: