- Home /
C# inheriting Awake()
I have a Parent class:
public class Base : MonoBehaviour
{
protected void Awake()
{
print("Do some stuff");
}
}
And a child class that derives from Base:
public class Sub : Base
{
void Awake()
{
print("ALSO do this stuff!");
}
}
Unfortunately I'm still fairly new to programming and do not understand types such as abstract, virtual, etc. I only understand how to use private, public, and protected.
If you haven't figured it out already, what I want is for a base class to have something to do. Any class that derives from this should also do these things, and then do more stuff after it. I need this in the Awake() method so that it gets called when the object is instantiated.
Thanks for any help!
Answer by numberkruncher · May 08, 2013 at 08:40 PM
public class Base : MonoBehaviour {
protected virtual void Awake() {
print("Do some stuff");
}
}
public class Sub : Base {
protected override void Awake() {
base.Awake();
print("ALSO do this stuff!");
}
}
@numberkruncher's answer is correct. Check out this $$anonymous$$SDN doc on polymorphism to understand why:
http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.80%29.aspx
Basically, your parent function needs to be marked as virtual, and the child as override. They will both also need the same access modifier, either protected or public.
Yeah I only just found that stuff one $$anonymous$$ute ago, marking you as correct answer
Your answer
![](https://koobas.hobune.stream/wayback/20220613101841im_/https://answers.unity.com/themes/thub/images/avi.jpg)