- Home /
OnTriggerEnter2D(collidertype coll) C#
Hi, I'm very new to the unity engine and I'd like to ask the community a simple question that bugs me a bit. I've studied c# with console programs and then moved to forms, and i'm generally used to create the "engine" of my applications, this is why I find it hard to understand for example how this method works.
What is exactly that calls this method and why? A particular delegate/event ? Is there some documentation that explain exactly how those kinds of methods works/gets called by the unity engine ?
Most important : If that is an inherited method from monobehaviour, why we're not supposed to override it ?
Answer by Hoeloe · Mar 31, 2014 at 10:31 PM
It's a method that is called when a collider with the "trigger" tag set collides with the collider attached to the same object as the script (assuming at least one of the colliding objects has a Rigidbody2D attached). It is only called once, when the colliders touch for the first time, and will not be called again between those two colliders until they separate. The purpose of this is if you want a one-time action to occur when the colliders first touch.
The reason you don't have to override it is because MonoBehaviour is not really a C# class. It's actually a C# hook into C++, and it's the C++ code that eventually calls the method. Unity's base engine is written in C++ (for speed), and the scripts you write are attached onto that engine, meaning that some of the language constraints you find in C# aren't retained between the language. It's a little messy, like a lot of Unity, and could be done in a neater and more consistent way, but that's the explanation, since you asked for it.
Thanks a lot, things are way clearer now.. Well it's a bit frustrating that I cannot completely understand why I'm doing something but I got to do it anyway since "that's how it works"
What don't you understand? It's perfectly simple. Unity is running the base engine in compiled C++ (which is really just whatever machine code your OS uses) underneath your script. It hooks into your scripts when it needs to, but because it's not bound by C#s rules of inheritance, it just jumps straight to whatever it needs. This is why you don't need override, and why it can access the methods even though they're private. Yes, it's a little messy, but how it works is perfectly understandable.
Thanks again, I believe things will be even more clear as soon as i'll jump into c++
You don't need to know anything about C++ to understand this. The thing is, most of C#'s protection systems, like access modifiers and overrides, are dealt with by the compiler - they disappear once the code is compiled. If code has been written to access it externally, interfacing with the compiled code, not the source, then you can bypass the protection schemes. This is what has been done. The fact that it happens to be done in C++ is almost irrelevant, the only reason it's not entirely is that C++ allows you to use arbitrary pointers, which makes this possible.
Your answer
Follow this Question
Related Questions
Scale fit? 1 Answer
How do i use monodevelop 3.0.6 with unity 0 Answers
Issues using the 'new' keyword 3 Answers
My die function gets called without me asking it. 1 Answer
How can i shuffle a list 5 Answers