Book Image

Learning C# by Developing Games with Unity 3D Beginner's Guide

By : Terry Norton
Book Image

Learning C# by Developing Games with Unity 3D Beginner's Guide

By: Terry Norton

Overview of this book

For the absolute beginner to any concept of programming, writing a script can appear to be an impossible hurdle to overcome. The truth is, there are only three simple concepts to understand: 1) having some type of information; 2) using the information; and 3) communicating the information. Each of these concepts is very simple and extremely important. These three concepts are combined to access the feature set provided by Unity. "Learning C# by Developing Games with Unity 3D Beginner's Guide" assumes that you know nothing about programming concepts. First you will learn the absolute basics of programming using everyday examples that you already know. As you progress through the book, you will find that C# is not a foreign language after all, because you already know the words. With a few keywords and using substitution, before you know it, you'll be thinking in code. The book starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it's needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity's Scripting Reference. The first half of this book is devoted to the code writing beginner. The concepts of variables, methods, Dot Syntax, and decision processing are fully explained. Since C# is an actual programming language, we take advantage of this to develop a State Machine to help control and organize each phase of a Unity project. Once the basic programming concepts are established and we have some State Machine organization, the features and power of Unity are accessed using the Scripting Reference. If you're looking to learn C# for Unity then this is the book that offers everything you need and more - so discover what C# offers today and see your work come to life as complete games!
Table of Contents (21 chapters)
Learning C# by Developing Games with Unity 3D Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – modify StateManager


Edit the StateManager class to use the IStateBase interface. This allows the activeState variable to store all of the State class objects. Also add the code that does the switching to the next State:

  1. Modify StateManager as shown in the next screenshot.

  2. Remove the Debug.Log statement.

  3. Save all files.

  4. In Unity click on Play.

  5. Now press the Space bar key to cycle through the States.

What just happened?

The following is the output to the Console as you repeatedly press the Space bar key:

The State Machine starts with BeginState being active. Pressing the Space bar key makes PlayState the active State. Pressing the Space bar again makes WonState the active State, and then pressing the Space bar key once more makes BeginState active again.

We now have a working State Machine. For the benefits a State Machine provides, there isn't much code involved to changing States.

Let us follow the code flow for switching States:

  • There are no States created as of yet

  • The StateManager...