FindObjectOfType or FindWithTag

I have a GameObject which I keep between all scenes (what I believe is referred to as a singleton), and grab a reference to it using FindObjectOfType in Awake.

But the Unity Script Reference says that function is “very slow”, so I’m just wondering if FindWithTag and then a GetComponent on the script would save a considerable amount of processing time?

1 Answer

1

“Slow” is relative. We’re talking about computers, where “slow” could mean doing something “only” 10,000 times per second instead of 10 million times. If you’re just doing it in Awake, it doesn’t matter even a little bit.

Ah, that's very helpful to know. Cheers Eric :)

–

As stated, it should be used in Awake or Start. You may use it in Update, which is called every frame, but will be a huge performance cost. It is always best to use that method to cache your objects once so you can then reference back to them at anytime during the program.

–

Yep. I only ever use Finds in Awake or Start, and I usually try to use editor references where possible.

–