A little confused by something

at about 4:50 of this video, Ben entered the line “levelManager = GameObject.FindObjectOfType();”, which I understand. But he put it in the OnTriggerEnter2D event. Should this not go in the Start() event? And if so, how come his worked?

Hello David,

My understanding is that it’s best practice to call FindObjectOfType only once in Awake or Start instead of calling it each time the event fires.

Happy Coding :slight_smile:

I’ve heard that good coding practice is to work as locally as possible. That means that, in the context of the script that you’re talking about, the levelManager is probably not needed outside of the trigger event. If this is the case, then there’s no need to declare it beforehand.

On the other hand if, let’s say, it also needed to be available for a button press or some other method, it would need to be declared at the beginning of the script so that it is accessible by the various methods.

If you were to place the levelManager reference in the OnTriggerEnter2D and then try to refer to it from a different method, it would not work because it has been defined locally, within the OnTriggerEnter2D method.

Given that it is a “levelManager” that you are referencing, there’s a good chance that you will not need to refer to it more than once (since you’re probably leaving the level when it fires), which resolves the conflict between my answer and Justin’s answer.

I hope that helps.

Great answer, Michael!

@David_Roddick, I should expand on my original answer to be clear. In this specific example, we can depend on the event to fire only when the ball goes past the paddle, thus calling FindObjectOfType only once.

However, this is not a practice you want to make a habit of. Why? Because Find methods in Unity are not cheap. Calling them in update frames / events can have a negative impact on performance. This is why you’ll see the general recommendation is to find what you need in Awake / Start. Here’s a quote from the Unity documentation on FindObjectOfType.

Please note that this function is very slow.

For the sake of consistency and good practice, I recommend calling Find methods like FindObjectOfType in Awake or Start because it better prepares you for future changes to your scripts.

An example: your next feature is to allow the player to have multiple attempts before the game is over. If you’re not careful, you may leave the event where it’s calling FindObjectOfType every time a collision happens, even though you only intended it to be called when the final ball triggers the event. The potential for introducing this type of bug is eliminated by finding objects earlier.

In summary, take care of getting what you need early to ensure that there’s no way extra Find requests can be made to Unity. When you start working on larger games with more GameObjects in your scenes, these Find requests will be slower due to the need to search through more objects.

I hope this helps.

Okay, so I guess that means I was right, it should go in the Start (or Awake) event, instead of in the OnTriggerEnter2D event, where he put it.

I thought so.

Privacy & Terms