I can't make protected inheritance work

Hi! I have a simple snippet of code that represents what my issue is without showing the entire code I have. I have a protected “key” variable in my base class named State. In each entities, there is a dictionnary of states.

When I create these states, I declare them by their specific type (Sub_State_1, Sub_State_2) that derive from the State class. Right after, in the entity class, I try to set the key value in the State class (I tried a bunch more methods at this point, I’m leaving it that way as a placeholder).

The problem is, when the function called update_state is run, the key variable is still set to null as if it did not inherit from the base class. And I understand that it might not inherit it after initialization. But I really need it to be the SAME value in both classes (State and Sub_State_X). Also, yes, I tried changing it with a function call like “change_key_in_state” to avoid errors in some of my cases.

I would like to avoid sending it as a parameter manually to each substate to avoid manual errors and cumbersome code. Automatically updating it in the awake function insures this “synchronization”.

So yeah, long message, trying my best to make this understandable. I’m stuck and I can’t find the exact resource online to help me with this (I looked).

public class State {
	protected string key;

	public virtual void update_state () {

	}
}

public class Sub_State_1 : State () {
	public Sub_State_1 () {

	}

	public override void update_state () {
		base.update_state();
		Debug.Log(key); // I want it to not be null
	}
}

public class Sub_State_2 : State () {
	public Sub_State_2 () {

	}

	public override void update_state () {
		base.update_state();
		Debug.Log(key); // I want it to not be null
	}
}

public class Entity  {
	public Dictionary<string, State> states;

	void Awake () {
		states = new Dictionary<string, State>();
		states.Add("Idle", new Sub_State_1());
		states.Add("Run", new Sub_State_2());

		foreach (KeyValuePair<string, State> state in states) {
			state.Value.key = state.Key;
		}
	}

	void Update () {
		foreach (KeyValuePair<string, State> state in states) {
			state.update_state();
		}
	}
}

Well, you can’t assign the “key” variable from the Entity class, because it’s protected. Make it public or create a public method to do it.

Second thing is that, in foreach loop, you try to access state.update, when you shoud access state.Value.update.

I’ve corrected the code above so it works and Console shows a lot of “Run” and “Idle” messages.

using System.Collections.Generic;
using UnityEngine;

public class State
{
    public string Key;

    public virtual void update_state()
    {
    }
}

public class Sub_State_1 : State
{
    public Sub_State_1()
    {
    }

    public override void update_state()
    {
        base.update_state();
        Debug.Log(Key); // I want it to not be null
    }
}

public class Sub_State_2 : State
{
    public Sub_State_2()
    {
    }

    public override void update_state()
    {
        base.update_state();
        Debug.Log(Key); // I want it to not be null
    }
}

public class Entity : MonoBehaviour
{
    public Dictionary<string, State> states;

    private void Awake()
    {
        states = new Dictionary<string, State>();
        states.Add("Idle", new Sub_State_1());
        states.Add("Run", new Sub_State_2());

        foreach (KeyValuePair<string, State> state in states)
        {
            state.Value.Key = state.Key;
        }
    }

    private void Update()
    {
        foreach (KeyValuePair<string, State> state in states)
        {
            state.Value.update_state();
        }
    }
}