How do i make my own On(somethingSomething)() function?

Like OnTriggerEnter(), OnInspectorGUI(), Start(). How do i make my own version of this in my own script?
For example: OnDifferentHotBarSlotSelected(), when a player scrolls their mouse wheel, this function should call. But then how do i make that happen?

Thanks in advance.

I think i found a solution. Use SendMessage(“Name”) to call all function with that name on this gameobject.

So, in the collider script, the one made by unity, SendMessage(“OnTriggerEnter”)" is probably stated.

I’m not sure if you can make Like OnTriggerEnter, You

` void Update() {
// call the function (play it)
OnDifferentHotBarSlotSelected();
}

void OnDifferentHotBarSelected() {
// if player scrolls
}`

Well you just have to write them into your code, at the moment that something happens. Let’s see the following example:

int amount = 0;

public void Update(){
	if(Input.GetKeyDown("a")){
		AddToAmount(2);
	}
}

public void AddToAmount(int value){
	amount += value;
	OnAddToAmount();
	//Or you could pass a parameter too:
	OnAddToAmount(value);
}

public void OnAddToAmount(){
	Debug.Log("A value has been added to amount!);
}

public void OnAddToAmount(int value){
	Debug.Log(value + " has been added to amount!);
}

So to relate this to the hot bar selection, you will need to find the code for that selection. In your code there will be a line that shifts the selection from one to the next. Maybe a simple counter keeps track of it. After the selection has been changed, just paste your custom OnX() underneath it.

As I mentioned in my comment, Unity’s own “event messages” are handled quite differently compared to SendMessage. Also note that not all methods which start with “On” are “magic method names” which Unity calls magically through reflection. Since you mention OnInspectorGUI, it’s an actual virtual method of the Editor class which you can properly override.

I would highly suggest instead of using SendMessage to use interfaces. For example:

public interface IHotbarReceiver
{
    void OnDifferentHotBarSlotSelected()
}

Now other scripts which want to receive this “event” can simply implement that interface

public class SomeReceiverScript : MonoBehaviour, IHotbarReceiver
{
    public void OnDifferentHotBarSlotSelected()
    {
        Debug.Log("selected slot changed");
    }
}

From your hotbar script you can just use GetComponent / GetComponents with your interface type.

var hotbarReceiver = GetComponent<IHotbarReceiver>();
if (hotbarReceiver != null)
    hotbarReceiver.OnDifferentHotBarSlotSelected();

Of course this can only reach a single target script. To call the method on all scripts on the same gameobject you would use something like:

var hotbarReceivers = GetComponents<IHotbarReceiver>();
foreach(var hr in hotbarReceivers)
    hr.OnDifferentHotBarSlotSelected();