# Inheritance in c#

**URL:** https://discussions.unity.com/t/inheritance-in-c/764531
**Category:** C# & .NET
**Tags:** Scripting, Mono
**Created:** [November 6, 2019, 7:32am UTC](https://discussions.unity.com/t/inheritance-in-c/764531 "2019-11-06T07:32:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![WR-reddevil](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/wr-reddevil/32/532432_2.png) [@WR-reddevil](https://discussions.unity.com/u/WR-reddevil)
#### Post date: [November 6, 2019, 7:32am UTC](https://discussions.unity.com/t/inheritance-in-c/764531/1 "2019-11-06T07:32:14Z")

</div>

I have two classes “Parent” and “Child”. “Parent” has been inherited in “Child” class.

here is the code

```csharp
public class ParentClass : MonoBehaviour
{
    public int thisis;
    // Start is called before the first frame update
    void Start()
    {
        thisis = 5;
    }
}

```

```csharp
public class ChildClass : ParentClass
{
    void Start()
    {
        Debug.Log(thisis);
    }
}

```

when you run this code the value of “thisis” will be 0 and I want is to update it live.

![](https://pasteboard.co/IFoMVEa.png)

---

<div class="post-metadata">

### Author: ![mirkojpn](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@mirkojpn](https://discussions.unity.com/u/mirkojpn)
#### Post date: [November 6, 2019, 8:32am UTC](https://discussions.unity.com/t/inheritance-in-c/764531/2 "2019-11-06T08:32:41Z")

</div>

> [@WR-reddevil](#):
>
> I have two classes “Parent” and “Child”. “Parent” has been inherited in “Child” class.
> 
> here is the code
> 
> ```csharp
> public class ParentClass : MonoBehaviour
> {
> public int thisis;
> // Start is called before the first frame update
> void Start()
> {
> thisis = 5;
> }
> }
> 
> ```
> 
> ```csharp
> public class ChildClass : ParentClass
> {
> void Start()
> {
> Debug.Log(thisis);
> }
> }
> 
> ```
> 
> when you run this code the value of “thisis” will be 0 and I want is to update it live.
> 
> ![](https://pasteboard.co/IFoMVEa.png)

i don’t know if i catch it, but you should use

```csharp
protected virtual void Start()
{
}

```

on superclass (parent( and

```csharp
protected override void Start()
{
}

```

on your child

then just call base.Start() on the child

---

<div class="post-metadata">

### Author: ![WR-reddevil](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/wr-reddevil/32/532432_2.png) [@WR-reddevil](https://discussions.unity.com/u/WR-reddevil)
#### Post date: [November 6, 2019, 9:53am UTC](https://discussions.unity.com/t/inheritance-in-c/764531/3 "2019-11-06T09:53:56Z")

</div>

> [@mirkojpn](#):
>
> i don’t know if i catch it, but you should use
> 
> ```csharp
> protected virtual void Start()
> {
> }
> 
> ```
> 
> on superclass (parent( and
> 
> ```csharp
> protected override void Start()
> {
> }
> 
> ```
> 
> on your child
> 
> then just call base.Start() on the child

Yes, this will work but only when I manually call it.  
is there a way to update values without calling override method? or there is no way?

---

<div class="post-metadata">

### Author: ![csofranz](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@csofranz](https://discussions.unity.com/u/csofranz)
#### Post date: [November 6, 2019, 10:25am UTC](https://discussions.unity.com/t/inheritance-in-c/764531/4 "2019-11-06T10:25:49Z")

</div>

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

```csharp
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

```csharp
public class ChildClass : ParentClass
{
    public override void Start()
    {
        base.Start();
        Debug.Log(thisis);
    }
}

```

---

<div class="post-metadata">

### Author: ![Mordus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mordus/32/286706_2.png) [@Mordus](https://discussions.unity.com/u/Mordus)
#### Post date: [November 6, 2019, 10:45am UTC](https://discussions.unity.com/t/inheritance-in-c/764531/5 "2019-11-06T10:45:21Z")

</div>

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.
