Serializable attribute defaulting on play

Ok my problem is I have got 2 classes lets call them StateMachine and State

Now I have a class which derives from state machine which holds multiple different classes deriving from State. Example …

PlayerIdle : State // this class is set as [System.Serializable]
PlayerWalk : State // this class is also set as [System.Serializable]

These 2 classes are deriving from State

Player : StateMachine

player derives from state machine and holds public instances of both states.
The problem is that in the inspector I can drop down for example … PlayerWalk and I can see the attribute “walkSpeed” as it is set to public in the PlayerWalk class.

When I set these in the inspector however they jump back to a default value when I click play. Now both “State” class and “PlayerWalk” class are set as [System.Serializable] so why are they defaulting ? Thankyou

It’s hard to guess what you got wrong. From what you write, I suppose this is what you wanted:

public class StateMachine { public bool bar; }

public class State { public bool foo; }

public class PlayerIdle : State { public float idleTime = 0; }

public class PlayerWalk : State { public int walkSpeed = 1; }

[System.Serializable] // may not be needed
public class Player : StateMachine {
	public PlayerIdle idle = new PlayerIdle();
	public PlayerWalk walk = new PlayerWalk();

Then you have 2 options:

  1. Add a new GameObject on scene and then “AddComponentPlayer in it. In this case, you don’t need the 3rd Serializable there. Or;

  2. Simply add this parameter in any component already on the scene:

    public Player player = new Player();

There is no reason the [System.Serializable] wouldn’t work. Try it.

Cool? Then you should definetly rewrite your question to become more readable! :wink: