Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Mar 18, 2020 at 09:04 AM · ontriggerentermonobehaviouroninspectorgui

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Mar 18, 2020 at 09:52 AM 0
Share

$$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.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 18, 2020 at 12:40 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer

OnTriggerEnter without RigidBody 3 Answers

onFilterAudioRead Problems 0 Answers

Changing specific materials onTriggerEnter 3 Answers

Problem OnTriggerEnter is calling more than once 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges