I have this class Scrap that contains a UnityEvent named “myUnityEvent”. This event should be invoked with one argument. I write a code that will invoke this event when the spacebar is pressed.
using UnityEngine;
using UnityEngine.Events;
[System.Serializable]
public class UnityEventTakingOneArgument : UnityEvent<int>{}
public class Scrap : MonoBehaviour{
public UnityEventTakingOneArgument myUnityEvent;
void Awake(){
//myUnityEvent.AddListener(funcTakingNoArgument);
//Oops! Cannot do this, so let's do it in the inspector instead!
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space))
myUnityEvent.Invoke(42);
}
public void funcTakingNoArgument(){
Debug.Log("Ahem! I don't take argument, but I get called!");
}
}
But I can go to the Editor and add a function taking no argument to this UnityEvent (There are even functions taking string argument for you to choose):
Then when I press spacebar, this function taking no argument actually gets called.
So my questions are
- Why does Unity allows me to add function taking no argument or wrong type argument to my UnityEvent via the Editor?
- As I cannot add function taking no argument to UnityEvent taking 1 argument via code, how does Unity do it?
- When this happens and my UnityEvent gets invoked, what exactly happens to my argument (42).
I tried to look for documentation, but haven’t found clear explanation about this. So I would also appreciate links to relavant documentation.