Hi,
I have couple of questions about MonoBehavior class.I understand that any new class in Unity inherits from MonoBehaviour,but:
1.How come automatically shows only Start() and Update() method at the new class we create.
(I believe there are more then two methods)
2.Why other methods are not shown or other members .Let’s say a variable to be inherited to new script.
3.Why Start() methods runs only once in a script and Update() runs for each frame.
4.If I create a new script and put some methods and I want to be inherited by new scipt where some of the methods there will be shown and some not or some methods repeat per frame and some rund only once,How can I do that?
-
That is simply a template file that Unity choses when you make a new script. You can delete both of those functions if you are not using them.
-
There are too many to show, and most of the time you only use a few.
-
By design. Google around for Unity timing and object lifetime charts and try to understand WHY those choices were made; that will help you use Unity.
-
C# inheritance works as expected. There are plenty of tutorials online. You can call any function from Start() or Update().
not entirely true… you can have “normal” classes too, but if you want to hook into the unityengine functions or game loop you’ll need to inherit from monobehaviour.
LeftyRighty.I guess you can help me more.
Let’s say every new script has automatically Start() and Update().
If I add my own method ,MyOwnMethod() and want all new scripts when created to have Start,Update and MyOwnMethod methods.Is that possible and how can I modify?
think the paths in this thread are still correct:
just be aware that if you update your unity version it might overwrite the template with the standard one.
All the defined messages are listed under “Messages” here.
public class CustomMonobehaviour: Monobehaviour
{
protected virtual void Start()
{
Debug.Log("Starting CustomMonobehaviour");
}
protected virtual void MyOwnMethod()
{
}
}
public class OtherClass : CustomMonobehaviour
{
protected override void Start()
{
base.Start();
Debug.Log("Starting OtherClass");
}
protected override void MyOwnMethod()
{
base.MyOwnMethod();
Debug.Log("MyOwnMethod");
}
}
Also you can create your class with MyOwnMethod() and override it. Also note that I’m using and overriding the Start() method, and unity will correctly invoke it.
thanks buddy