Gameplay is usually the main thing everyone thinks of when first undertaking the creation of a game. Build a scene, fill it with GameObjects, and write the scripts. After the game is well along in its development, a realization begins to appear that there's more to a game than just playing it. Putting a game on the market means it has to include things such as beginning title screen, the game options, or a player settings menu. Also, what happens when the user wins the game, or loses the game? It's one thing to write the code for GameObjects, but what about the code to add on all the other parts that come before and after the actual playing of the game?
In this chapter, we'll begin work on a C# State Machine, one possible solution for providing the necessary base code to easily incorporate the non-playing parts of a game.
In this chapter we will discuss the following topics:
So let's start to glue things together.
You learned the fundamentals of C# coding in the previous six chapters which were used for creating Components. You will use the same coding fundamentals to create a State Machine. You have learned in Chapter 6, Using Dot Syntax for Object Communication, that an object created from a class is called an instance of the class. In Unity, an object created from a class is called a Component. These terms may be different but they mean exactly the same; an object is created as defined by a class.
However, there is a difference in the way the traditional C# environment of the State Machine gets an instance object created, and the way Unity gets a component object created. The difference is as follows:
Have no fear! It's very similar to the code in Chapter 5, Making Decisions in Code, we used to create a List
and Dictionary
object.
18.226.251.70