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.
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.
I should add, continuing your example, the method OnGroundedStay() should be declared public, like: public void OnGroundedStay ()
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.
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.
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.
Your answer
Follow this Question
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