What's faster: referencing an object, loading an object, or creating an object?

I don’t like referencing public objects handed from the editor, even prefabs, in my scripts unless I’m just testing things out. This is because I don’t like having to figure out where all my broken references are when I change/rearrange things. I don’t like the dependency. So I basically always use Resources.Load or create new GameObjects from scratch at runtime.

Does anybody know how the performance of referencing objects vs. creating vs. loading them adds up?

1 Answer

1

Referencing objects are practically free when compared to generating them. But generating game object are not going to cost you much, unless you’re doing it a lot.

On creating vs. loading, I assume it depends on the object in question, but when you load stuff from Resources, that means that Unity has to parse that data and turn it into a game object, so it will probably be slower than generating it. But again, unless this is something that’s happening a lot, the difference will probably not be noticeable.

The big issue with the Resources folder is that Unity can’t really figure out what you need in it, since that stuff is loaded from scripts by strings. This means that everything you put into the resources folder will be included when you package the project, no matter if you use it or not. Files in any other folder will only be included if they’re used in any scene. So be careful about the Resources folder.

That was a very thorough comment. Probably could qualify as an answer...