Skip to content

Instantly share code, notes, and snippets.

@joobei
Created July 27, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joobei/324d6998f01812ea589fb1047e34d4a5 to your computer and use it in GitHub Desktop.
Save joobei/324d6998f01812ea589fb1047e34d4a5 to your computer and use it in GitHub Desktop.
Monobehaviour problem
class State: Monobehaviour {
//reference to the state we will need to jump to
public State nextState;
protected virtual void Update()
{
if (mouseClicked)
{
doSomething();
advance();
}
}
protected abstract void doSomething();
public virtual void advanceState()
{
//disable this state (i.e. update will stop being called)
enabled = false;
//enable the next one
nextState.Activate();
}
public virtual void Activate()
{
this.enabled = true;
}
}
//First child state
class FirstState: State {
protected override void doSomething()
{
Debug.Log("FirstState");
}
}
//second child state
class SecondState: State {
protected override void doSomething()
{
Debug.Log("SecondState");
}
}
//output in console when advancing from firstState to secondState:
FirstState
SecondState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment