How does this behave different in Start or Update?

Could you please briefly explain the differences in how this collision trigger behaves when we put it where we have it in the video, in start, or in update?

Hi,

What is “this”? Could you please share the relevant code you’d like to have explained?


See also:

I was talking about the code in this video :Triggers To Restart Level | GameDev.tv

It’s since changed on my end, but basically he removed the start and update methods, which I don’t see done much, and I was wondering what would be different if we put the code in the start and update methods

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CrashDetector : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Ground")
        {
            Debug.Log("Head trigger works");
        }

    }
}

OnTriggerEnter2D(Collider2D other) is what is called a callback. This basically means, if the conditions are met and some kind of event happens (like something bumping into the collider) it’ll call this method.

If you put the code in the start or update methods, A). It’d get called without the event occuring, so how would you know if it happened? and B). It wouldn’t be sent the collider2D information about the collision, so you couldn’t be able to check if the other.tag was the ground.

1 Like

Great explaintion thanks!

1 Like

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

Privacy & Terms