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 /
  • Help Room /
avatar image
0
Question by antiquote · Jun 17, 2016 at 07:02 PM · c#workflowstates

How to define a function seperate from the class in which it must called?

What I wish to achieve is similar to the functions that Unity has already provided for us, such as their Update functions. Where they must be called is already placed, and we define their tasks. However, it still carries out all definitions of that function. For additional context, I am writing a base script for player movement and would like to write state methods, if that makes sense. For example:

 public class Example : MonoBehaviour {
 
     if( grounded )
     {
         OnGroundedStay();
     }
 }
 
 public class SeperateScript : MonoBehaviour {
 
     void OnGroundedStay () 
     {
         //Perform unique tasks when we remain on the ground.
     }

Thank your for your time, and I apologize for the bluntness of the explanation.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by btmedia14 · Jun 17, 2016 at 08:12 PM

To call the method/function OnGroundedStay() a reference is first required to the class in which it is defined. The class Example could be something like:

  public class Example : MonoBehaviour {
  
      if( grounded )
      {
          SeparateScript otherScript = new SeparateScript();     //Creates reference to other class
          otherScript.OnGroundedStay();                                      //Invoke specific method in class
      }
  }

Essentially its the class reference that is needed and then call any of the public methods within it. Note, private methods of referenced class cannot be accessed outside of the class.

Comment
Add comment · Show 7 · 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 btmedia14 · Jun 17, 2016 at 08:14 PM 0
Share

I should add, continuing your example, the method OnGroundedStay() should be declared public, like: public void OnGroundedStay ()

avatar image antiquote · Jun 17, 2016 at 08:31 PM 0
Share

Thank you, this much is understood. However, if I were to define it "OnGroundedStay()" within an additional seperate script, I would have to reference the additional seperate script as well. $$anonymous$$y curiosity lies within the "Update()" function and if a similar effect may be achieved.

Where "Update()" can be defined within every script, and yet we can still rely on the same call order, while also having several of these instances carry out their own "Update()"

I apologize if I'm not being very clear. $$anonymous$$ore examples would be, "OnCollisionEnter()", "OnTriggerStay()." Like an event system.

avatar image btmedia14 antiquote · Jun 17, 2016 at 09:53 PM 1
Share

If I understand correctly, the requirement is an ability to invoke an additional set of functions from a variety of scripts in a manner to similar to calling the Update() or Start() type functions without any prefix. The classes in Unity are by default derived from a base class called $$anonymous$$onoBehaviour. This $$anonymous$$onoBehaviour class contains a large set of methods, such as Update() and Start(). When a new class is created, such as the Example class it will automatically have access to all the parent methods in $$anonymous$$onoBehaviour since $$anonymous$$onoBehaviour is the base class, or parent class. As such there is no need to explicitly reference the parent class, its done under the covers by the compiler.

Think of classes as containers. Any new functions need to be placed in a container, i.e. a containing class. Any other classes then require to refer to that container, the class reference, in order to invoke its methods.

If the function OnGroundedStay() did not have the reference prefix it would imply either the function is in the same calling class or it has to be in the parent. $$anonymous$$onoBehaviour is a Unity base class, and as far as I know, we are unable to append directly to its list of functions.

Typically functions, even a set of generic commonly used functions, should be contained inside a class (or several such classes) which is then provided to other consumer classes. Hope this is clear.

avatar image antiquote btmedia14 · Jun 18, 2016 at 06:48 PM 0
Share

Apologies for the immense delay. However, so if I wanted to implement this ability on my own, it would simply require declaring the functions within the base class (inheriting from $$anonymous$$onoBehaviour), calling them within their respective locations in the base class, and then having a seperate class that inherits the base to define them to which they so desire and every instance that the funciton is defined, it will carry out that function?

 public class Base : $$anonymous$$onoBehaviour {
 
     protected void OnGroundedStay ();

     void Update () {
             //For the sake of the example.
             OnGroundedStay();
     }
 }
 
 public class SeperateClass : Base {
 
     protected void OnGroundedStay ()
     {
         //Let's say jump here.
     }
 }
 
 public class AnotherSeperateClass : Base {
     
     protected void OnGroundedStay ()
     {
         //And fly here.
     }
 }
 

You are being immensly helpful, and I appreciate the thorough responses and patience.

Show more comments

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

168 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 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

change selected state between x gameobjects 0 Answers

Remember If Animation Was On/Off On Scene Switching 0 Answers

NetworkBroadcastResult.broadcastData having trouble converting byte array to string 0 Answers

Player is Going Through Walls 2 Answers

NullReferenceException: Object reference not set to an instance of an object (Unity UI) 0 Answers


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