State-driven behavior DIY states
Games as a whole, and individual objects or characters, can often be thought of (or modeled as) passing through different states or modes. Modeling states and changes of state (due to events or game conditions) is a very common way to manage the complexity of games and game components. In this recipe, we create a simple three-state game (game playing/game won/game lost) using a GameManager
class. Buttons and a timer are provided to simulate the events that would allow a player to win or lose the game:

How to do it...
To use states to manage object behavior, follow these steps:
- Create two UI Buttons at the top-middle of the screen. Name one
Button-win
and edit its text to readWin Game
. Name the secondButton-lose
and edit its text to readLose Game
. - Create a UI Text object at the top left of the screen. Name this
Text-state-messages
, and set itsRect Transform
height property to300
and itsText (Script) Paragraph Vertical Overflow
property toOverflow
. - Create...