State Pattern Objects and the Inspector Window

I’m a fan of using the state pattern to control how characters (among other things) behave in-game. Right now when I want to set a character’s state, I’ll call something like character.State = new AttackState(enemy), or character.State = new WaitState(2.0f, new FinishedWaitingState()) – nothing fancy, but generally a state object that takes parameters.

I’d like to set it up so that I can assign a character’s initial state through the Editor and set up its initial parameters. However, the issue I’m facing is that even after [System.Serializable]ing my CharacterState class, there’s still no objects for me to actually reference at Editor-time. Right now, I have a less than favorable solution where I have a corresponding enum to every State that is visible on the inspector, and am instantiating a new State based on that enum (with default values). I’d much prefer to actually reference an instance of the class at editor-time without doing something strange like deriving it from a Component and sticking it on a GameObject, though I’m not opposed to doing so if that’s as good as it’ll get.

Any insight is greatly appreciated!

Use a ScriptableObject. Unity’s serializer will preserve them as individual objects, and you can make use of polymorphism.