How expensive is hardcoding?

Simple question, just wanted to know if hardcoding object references, instead of finding them through FindGameObject, or other methods to find references inside code, is more or less expensive? And if so, how much more (not expecting numbers here, just ‘a lot more’ kind of thing)


You can’t “hardcode” references at all ^^. The term hardcoding isn’t appropriate here. Actually using “FindGameObject” is more like hardcoding. Using the serialization system and assigning the references in the inspector is much more flexible and usually much faster.

References which are serialized in a scene or prefab require almost no performance as the object IDs are most likely cached / hashed by the serialization system. In most cases the useage of any “search” method requires more runtime performance.

So my advice is:

  • If two or more objects in a scene require each other it’s best to assign those references in the inspector to public / serialized variables.
  • If an object that is required by another one is created dynamically (Instantiate) make sure your prefab variable type represents the most useful component / object on the prefab’s root. Instantiate will always return the same object type that you passed in. If you pass a Transform reference it will return the Transform of the instantiated object. If you pass a “PlayerScript” reference you get a “PlayerScript” reference back. That way you get access to an component on the newly created object for free. In my opinion “GameObject” is the most useless type as you can’t do much with a GameObject itself. “Transform” is in a lot cases the better choice.
  • If the required object(s) aren’t known beforehand / are created somewhere else, there’s no way around using either a search function or some kind of singleton pattern.

As you might have read in the docs most “Find” methods are quite slow as they don’t recommend to use them every frame. So avoid them if possible.

A “hardcoded” or inspector drop-in reference will always be the faster than performing any operation like searching or getting a component and so on. However, usability and maintainability is much more important than performance for 90% percent of development time. You should chose a solution with good architecture/design and make your game. Once all features work, you can start optimizing it. At this point you almost never gain a single extra frame just by shifting around a few references vs method calls. Expensive things are those which take place thousands of times within a few frames like nested for loops and rendering pixels. Of course, if you have 500 GameObject.Find calls you’d gain a lot by replacing them, but that also means you have bad design, so basically back to the drawing board and just make the code feel organized and systematic. Just ask yourself: “What makes the most sense?” If you build prefabs and object constellations in the editor it makes the most sense to use inspector fields to drag and drop, but if those references have to change during runtime, you already know that you have to search for them via Type, Tag or Name.