Hey,
say i have a parent class A and a child class B that inherits from A.
class A has a void LateUpdate() implemented.
Will the parents LateUpdate() be called if the child does not explicitly call base.LateUpdate()?
In general, how are the Unity methods like Start(), Update() etc. handled in inherited functions?
Yes. Subclasses will run Unity built-ins from the parent. Script BB prints ub lua (it’s own Update, and the LateUpdate it “inherits”) over and over:
public class AA : MonoBehaviour {
void Update() { print ("ua"); }
void LateUpdate() { print ("lua"); }
}
public class BB : AA {
void Update() { print("ub"); }
}
Unity isn’t using inheritance rules to run them – it’s using Reflection tricks – which is why things seem to make no sense.
Yes. All members of the parent class are present in the child class. If you don’t want LateUpdate() to have an implementation in the child class, then you should declare the method as virtual and then override it with an empty body. In any case all Unity-specific methods are like any other method in C# so the same general rules apply.
The methods you mention are private, hence they are hidden when you sub-class your parent class. Unity will still call the implementation it finds so the parent class LateUpdate()
will be called (you can verify with a Debug.Log(...)
call in the parent class implementation).
However, if you do implement one of these methods and need also to call the base class implementation you will need to do it explicitly by using the base
keyword in your own implementation (other wise the parent implementation will not be called)