- Home /
Are parameters in Unity event callbacks optional?
Hi!
I just answered another topic and after posting it I reviewed it one more time to see it all fit together. I realized that I had omitted the parameter in OnTriggerExit, but the code still ran. I got the callback even though it doesn't conform to the signature of the callback.
It seems that:
void OnTriggerExit ( )
{
}
Is just as good as:
void OnTriggerExit ( Collider other )
{
}
Except that the former omits information and possibly make cleaner code, if the parameter isn't important. The latter is the definition found in the scripting reference documents.
So this is news for me. Are parameters optional in unity callbacks or is this a fault in Unity? Is there any danger omitting parameters? If there are I can see this a potential cause of bugs in scripts.
Answer by Eric5h5 · Dec 09, 2010 at 12:58 AM
I've heard it's better to omit the parameter if you're not using it because there's less overhead, however in practice there doesn't actually seem to be any difference, so possibly that's been optimized in the meantime. It doesn't hurt anything to leave it out, in any case.
@Statement: It applies to OnCollision stuff as well as OnTrigger; not sure about others.
Answer by Ashkan_gc · Mar 15, 2011 at 03:30 PM
they are not optional, the event callbacks are not optional too. see you can have or don't have any event you want so they are not abstract methods that you override or something like that. unity uses reflection to access those methods. so in collision it will check all of your scripts to see weather you have any method called OnTriggerEnter, if yes then it checks if you want an argument or not, if yes it will fill it for you and if no, it will not fill your parameter and just call the function. so it's faster to don't have the parameter if you don't want it. you can omit any parameter of any message in unity. (i mean messages like OnTriggerEnter and not those normal methods that you call with SendMessage).
Well, from the rest of the discussion after "they are not optional" you're saying basically that they are optional. If I don't want to use a parameter and omit it, then it is optionally discarded since it check the method type it's about to call, and discards the parameter if its not in the signature.
yes but i ment the technical term "optional parameter", i just would like to make it clear that how it works and why you can have it with/without parameter and what is the real diference.
Your answer
Follow this Question
Related Questions
Trouble setting a parameter with a collision 1 Answer
How to get Animator.GetBool to work as a parameter in OnCollisionEnter in Unity (c#)? 0 Answers
Script Error (EnemyHeal): Update() can not take parameters. 1 Answer
OnCollisionEnter not working? 1 Answer
Why OnCollisionEnter cannot detect collisions between colliders? 1 Answer