Getting collision info into the update function

void OnCollisionExit2D(Collision2D col)
    {
         if (col.gameObject.CompareTag("Pan"))// && stay== 0)
         {

         }

    }

    void Update()
    {

    }

How would I get the col value from the on collision exit into the update function?

You add an attribute to the script where you can store it. You add that above your methods.

void OnCollisionExit2D(Collision2D col)
    {
Collision2d collision;
         if (col.gameObject.CompareTag("Pan"))// && stay== 0)
         {
                 collision = col;
         }
    }
    void Update()
    {
    }

That way, you can access it inside Update. Though if you need the gameobject, not the collision event itself, it’d best be to save the gameobject, not the collision.

1 Like

Thank you so much, I was stuck on this for a while and couldn’t find anything on the internet.

Oh wait, I made an error, the “Collider2D collider;” needs to be outside the method, not inside. Just in case you didn’t figure that out yourself.

1 Like

I figured it out, but thanks for clarifying.

@Neomedus Glad you are operational. This might be obvious too, but one handy thing I like to do with this pattern is in Update() when I notice that the collision has been set and take action, it’s handy to also set it back to null, to signal that the Update function has basically “consumed” the event.

1 Like