- Home /
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
Answer by cregox · Dec 02, 2013 at 05:31 PM
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; }
[System.Serializable]
public class PlayerIdle : State { public float idleTime = 0; }
[System.Serializable]
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:
Add a new
GameObject
on scene and then "`AddComponent`"Player
in it. In this case, you don't need the 3rdSerializable
there. Or;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! ;-)
Your answer
Follow this Question
Related Questions
Custom Display for system.object Editor 1 Answer
Logic Problems using for loops 0 Answers
Displaying objects in lists of lists. 2 Answers
Why aren't my editor values saving when I press play? 3 Answers