OnCollisionEnter2D question

Hi, I am totally newbie here for c# and I don’t grab the idea of OnCollisionEnter2D.
Is it a method? Do we have to declare it every time we use it? Say, if we declared it in LooseCollider script, why can’t we just recall it from there when we use it in Brick script?

It is an event-delegate structure built in monobehaviour. Whenever an object collides with other, the system triggers the OnCollisionEnter event and call the code block within it, there are many events like this one, such as OnMouseEnter, OnTriggerEnter etc

Thanks for fast response, appreciated. So you mean it is a function that is built in monobehaviour? Why can’t we just do, something like

if (OnCollisionEnter2D(Collision2D collision)){

Because the system looks for the method, if you put it within an if statement it won’t be able to call it, besides that the return type is void (don’t return anything).

Hmm… I think I’m kind of lost here. what do you mean by system looks for the method? do you mean system looks for OncollisionEnter2D method? or do you mean the OnCollisionEnter2D is not inside a method?

The Event structure have a function that is called when the collision happens, and this function points to OnCollisionEnter2D (if it is 2D) or OnCollisionEnter (if it is 3D), that is why it is called a Delegate method (it delegates what to call when the event happens).

Okay, so event structure is something like class?

It is a structure made of a couple of methods and variables inside a class (MonoBehaviour in this case). If you look at the declaration of your class, you will see that it has a “: MonoBehaviour”, this means that it inherits from the MonoBehaviour class. If you delete the : MonoBehaviour from the declaration of your class, you won’t be able to access things like Transform, Collisions, etc

Got that. Thanks for that. I guess I am not aware of the concept ‘structure’. I will look for that. thanks for your time

Oh… Now I understood the confusion, I mean structure as a generic term such as system, a collection of methods and variables, not the actual Stuct (which is a type and as you said, gets implemented similarly to a class and if you search for structure it will send you to the definition of a struct)

If you want to understand how the OnCollisionEnter works under the hood, the keywords that you should be searching for is C# Events

Oh okay. So just to wrap up here, Event Structure thing is, not a function(method) nor actual struct thing, but it is what it is that works like function? And we have to always declare it by using …

void Oncolider2D (collision2D coll) { }

Yes, it is a couple of methods and variables that works together to implement an expected behaviour.

The MonoBehaviour do something like this under the hood:

It implements an event handler that gets triggered when an collision happens, and when it gets triggered it will call an delegate method that handles the methods that should be called when this event happens, in this case it will call the method named OnCollisionEnter2D (Collision2D col), so that is why you have to write this specific method, it was already assigned within the MonoBehaviour class, otherwise the delegate won’t find it.

It is like saying:
“Hey, we have a collision in a 2D environment here, call the method named OnCollisionEnter2D If it exists”

1 Like

Okay… hmm… see, let say we do

void happy (){

inside certain class, and whenever we want to use that function, we do,


and that function will be recalled and work within same script that the class was declared or where class is recalled.

So my question would be, if Oncollider2D is a method that is included in class of monobehavior, why can’t we do the same? just doing,

OnCollisionEnter2D(Collision2D col);

something like that. Not as adding return type(void) before the function, meaning declaring it every time.

I see what you mean, but delegates and events points to methods, it don’t declare them, it will just look for a method with that signature inside the class that you create. It is an advanced topic and the syntax is different to what we will see in this course, you can take a look at how it works here:

Microsoft Events Tutorial

Each OnCollisionEnter2D only gets called when the object with this script attached is involved in a collision, and it is private (other methods outside the class don’t have access to it)

Okay now I get it very well. Many thanks for your time on this. Much appreciated!!

I’m glad for being able to help!

Privacy & Terms