FindObjectOfType question

I’m really enjoying this course as a total beginner and starting to feel like I can do some of the challenges. I programmed the scoring for instance before watching the lecture.

When I jump ahead and have a go, I usually declare the FindObjectOfType in the Start method, and then just use the variable in the method to access the method from another class.

Is this more efficient or does doing this add an extra step making it less so?

Rick uses GetComponent in Start, which is what made me think of this.

With these small programs we’re writing I’m sure it doesn’t make much difference but it would be good to know this for the future.

If I understand your question correctly:

Yes it’s generally more efficient. Any gameobject that you want to access more than once, it’s worth to assign via FindObjectOfType in the Start method, or, if there are multiple objects of that type in the scene, by using a SerializeField and dragging it in.

It doesn’t make a big difference on a small scale, but it does optimize things. Because everytime you use FindObjectOfType, that takes time and processing power. And if you’re gonna use the same game object multiple times, it’s much better to point a variable at it, instead of telling unity to search for it every time.

Thank you! That answers my question and confirms my thought.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms