If you want to extend (i.e. change the way it behaves) a method in a child class (like you are doing with Start() ), C# requires that you mark that method as ‘virtual’ in the parent, and ‘override’ in the child. If you override a method, and you want the parent’s code to be executed, you must call base..
so, the parent would be
public class ParentClass : MonoBehaviour
{
public int thisis;
// Start is called before the first frame update
public virtual void Start()
{
thisis = 5;
}
}
and the child becomes
public class ChildClass : ParentClass
{
public override void Start()
{
base.Start();
Debug.Log(thisis);
}
}
You can update anything you want in the parent class from the child class exactly the same as if it were in the child class itself (unless you declare it with the private access modifier, things declared without an access modifier are assumed to be private).
The problem with your code in your first post is that you have a seperate Start method in both the child and parent class.
Start on the parent class has no access modifier, therefor is assumed to be private and is not accessible from the child class. Which is why it’s not an error to also have a Start function on the child class (since it doesn’t conflict with the one on the parent class as it can’t access it anyway). Those are 2 completely different functions, one that only exists on the parent type and one that only exists on the child type.
When unity looks for a Start function to call it will find the one on the child class and stop looking. The one on the parent class will not be called. Since your parent class Start is never called a value is never assigned to your variable and so it will always be 0.
The solutions offered above solve this problem by having the parent class define a Start function in a way that is visible in types derived from it and the child class can extend and add functionality to instead of replacing it.