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 /
This question was closed Jan 06, 2017 at 01:21 AM by JoshBaz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JoshBaz · Dec 19, 2016 at 05:33 PM · triggereventevent triggeringcallmethods

How to create a custom "On..." method?

Hello. I would like to know how do I go about making a custom "On..." method. When I say an On method I mean a method like OnValueChanged or OnCollisionEnter. A method that is called in any class that has the method when a certain event occurs.

For example if I have a class for a rolling ball and the class keeps track of how much the ball moves and calls a method whenever it rolls 1 meter. And then in another class I could have the method "OnBallRollOneMeter ()" that gets called. The idea I have is like how OnCollisionEnter gets called whenever a rigidbody collides with something.

I just like to know if this is possible and could someone tell me how to do it. Thanks! (Preferably in C#)

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by LK84 · Dec 19, 2016 at 05:47 PM

You can use events.

First define a delegate: public delegate void BallRollHandler(object sender, EventArgs e) Then witin the rolling ball class you define public event BallRollHandler OnBallRoll; In your code when the ball has rolled 1 meter you can raise the event:

     if(distance==1)
     {
      if(OnBallRoll!=null)
     OnBallRoll(this,EventArgs.Empty);
     }

Note: Event Args don't need to be empty. You can use it to transfer all kind of data.

In the other class you first need to make a reference to the specific instance of the rollin ball class and then assign your method as a EventListener:

 [SerializeField] BallRollClass YourBallInstance;
 
 void Start()
 {
 YourBallInstance.OnBallRoll+=OnBallRollOneMeter;
 }
 
 void OnBallRollOneMeter(object sender, EventArgs e)
 {
 }
 

 






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 tanoshimi · Dec 19, 2016 at 05:54 PM

Also see:

  • https://unity3d.com/learn/tutorials/topics/scripting/events

  • https://unity3d.com/learn/tutorials/topics/scripting/delegates

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

Follow this Question

Answers Answers and Comments

76 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

Related Questions

On Void Called 2 Answers

How would I detect a right click with an event trigger? 3 Answers

Generalized call of specific methods 1 Answer

Event and methode names for C# 1 Answer

Using OnClick() with an empty GameObject 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