- Home /
How can I make Unity's overridable events appear within the Intellisense dropdown in Visual Studio (C#)?
I'm new to Unity, but familiar with C#. When I create a new C# script that opens in Visual Studio, I get Intellisense for everything except for overridable events.
For example, while working with collision, I expect to see events like "OnCollisionEnter", "OnCollisionExit", and "OnCollisionStay" within the dropdown. However, if I type "override", the only selections I see within the dropdown are "Equals(object o)", "ToHashCode()", and "ToString()".
Also, hovering over any term shows the usual tooltips, but hovering over something like "OnCollisionEnter" shows nothing at all. It's as if the system recognizes it, but won't tell me how or why.
What am I doing wrong?
Answer by Bunny83 · Feb 11, 2013 at 07:42 PM
Uhm you can't because they are not overridable functions ;). Unity send messages to your scripts. Unity even optimises this message sending that messages are only send to classes which have a certain method. The MonoBehaviour base class doesn't have all those callbacks.
When working with Unity you should always have your browser open with this page ;)
Correct me if I'm wrong, but it shouldn't be message sending like in Send$$anonymous$$essage
sense, rather it wires up the methods it knows on load, with reflection.. at least that's how it makes sense for me, because message sending in the Unity API sense is order of magnitudes slower than delegates. I'm curious too about how is this implemented in the background.
The way it most makes sense for me as to why it's designed this way is that big part of the API actually wraps native code, and the C++ base for the engine probably uses very different structuring in it's implementation, so it's not trivial or even impossible to make a wrapper for a class like Behaviour or $$anonymous$$onoBehaviour, simply because it doesn't exist in this logical form within the engine itself. The upside here is that the Unity API is much more than a wrapper, it exposes functionality in this very modular and straightforward way, and wires a whole bunch of stuff for free, so writing a script is like creating a $$anonymous$$i-wrapper for various functionalities of the engine core, and it seems like $$anonymous$$onoBehaviour is just a clever way for the compiler and the game coders to group functionality.
Yes, of course. Unity doesn't use Send$$anonymous$$essage for the events. Unity probably uses reflection at compile time and stores some meta data in the native code part of the object. In C++ there are no different classes, there's just a $$anonymous$$onoBehaviour object which is linked to a managed code instance of one of your custom classes.
They even change what happens in native code depending on how your callback function is declared. For example OnCollisionEnter has a Collision object as parameter. However if you omit the parameter in your callback, Unity won't even calculate all the stuff in that struct / class and don't pass anything as parameter. The same thing applies to a lot other callbacks.
The easiest way to see this optimisation is when you just delete the Start and Update functions from a class, so no callback is in the class, the "enabled" checkbox in the inspector will disappear since this class doesn't receive any callbacks.
Ahh, good to know. I probably just haven't hit that point in the tutorials yet. Thanks!
Your answer
Follow this Question
Related Questions
One listener for multiple objects? 2 Answers
Unable to step. Operation is not supported 0 Answers
Impossible to use Visual Studio 2015 Debugger anymore (Unity 5.2.1f) 2 Answers
csc.exe has stopped working ,csc.exe has stoped working 0 Answers
vector 3 isnt working offset part,the vector 3 isnt working 'offset' has a problem 2 Answers