State-driven behavior Do-It-Yourself 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 single GameManager
class.
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 read Win Game. Name the second Button-lose and edit its text to read Lose Game.
- Create a UI text object at the top left of the screen. Name this Text-state-messages, and set its Rect Transform height property to 300 and its Text (Script) Paragraph Vertical Overflow property to Overflow.
- Add the following C# script class
GameManager
to Main Camera:using UnityEngine; using System.Collections...