- Home /
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.
$$anonymous$$ost methods like OnTriggerEnter are provided by unity. You will have to write your own methods and call them when the current hotbar slot is changed.
This is basic C#, maybe you should start there.
Answer by harethl1s · Mar 18, 2020 at 09:36 AM
I'm not sure if you can make Like OnTriggerEnter, You
void Update() { // call the function (play it) OnDifferentHotBarSlotSelected(); } void OnDifferentHotBarSelected() { // if player scrolls }
Answer by $$anonymous$$ · Mar 18, 2020 at 10:11 AM
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.
Yes and no. ^^ Unity's physics system runs on the native engine side (C++ native code) and the events like OnTriggerEnter are directly invoked from the native side through reflection. Send$$anonymous$$essage achieves a similar result but is generally slower. For relatively rare events it shouldn't hurt too much. However using Send$$anonymous$$essage has several disadvantages. First of all since it uses a string variable to call the method it's relatively slow. $$anonymous$$isspelled / renamed methods would not be detected by the compiler and would cause an error at runtime.
Next Send$$anonymous$$essage can only take one parameter. While this is often enough it puts some restrictions on the signature. Also any value type parameters would need to be boxed and generate garbage each time you use Send$$anonymous$$essage.
Finally by default the "Send$$anonymous$$essageOptions" is set to "RequireReceiver". That means when you call Send$$anonymous$$essage but no script has a method that matches the specified name an error is generated. So you probably want to use Send$$anonymous$$essageOptions.DontRequireReceiver.
Note that "Send$$anonymous$$essage" is kinda legacy code. It can be used if you develop a completely seperate framework so other users can simply "define" the message method you call and it would work. However inside your own codebase it would make much more sense to use interfaces and direct method calls. Using Send$$anonymous$$essage is just lazy, slow and unsafe program$$anonymous$$g. There are many alternatives.
Answer by metalted · Mar 18, 2020 at 10:16 AM
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.
Answer by Bunny83 · Mar 18, 2020 at 12:56 PM
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();
Your answer
Follow this Question
Related Questions
MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer
OnTriggerEnter without RigidBody 3 Answers
onFilterAudioRead Problems 0 Answers