Chapter 5. Scripting the Game
Our game is really taking shape. So far, we've completed the game design, game environment, player character, non-player characters, and animations. That is a lot of work for just four chapters. There are a few things we need to accomplish in order to complete our game. First on this list is scripting the game.
In this chapter, we will write scripts for:
Managing the amount of water and corn that Colt collects
Monitoring how much water and corn each farm animal is given
Managing when a piglet becomes a pig and when a baby chick becomes an adult chicken
Managing when pigs and chickens reach blue ribbon status
Before we dive right into scripting, I'll provide a C# programming primer for those that need it. We'll also review how scripting is accomplished in Unity, where to find scripts, and how to use MonoDevelop.
After reading this chapter, you will:
Understand the fundamentals of C#
Be able to use MonoDevelop to create and edit scripts
Be able to locate scripts in a Unity...
C# is a comprehensive programming language. Teaching you how to use this language is beyond the scope of this book. I'll provide enough information in this section so that you can understand the custom scripts we'll be writing for our game.
Note
For a deeper understanding of how to program using the C# language, you can use one of the many books available from Packt Publishing on the subject.
C# is both a function-based and object-oriented language. Functions are reusable sections of code that we script. For example, if we create a script called feedPig, it might handle transferring water and corn from Colt to a pig. Since we have multiple pigs in our game, this function would probably get used a lot.
As I've mentioned, C# is also an object-oriented programming (OOP) language. OOP languages use classes, which are sets of data and functions. If we have a babyChick
class, for example, that class can hold data on how much corn the baby chick has eaten. That same class can...
When we are scripting, we need to use some sort of editor. We could use a standard text editor and achieve great results. But instead of using a text editor, programmers typically use a scripting editor. Scripting editors use color-coding that make the code more readable. They also provide spacing that makes looking at code blocks easier.
The good news is that Unity comes with MonoDevelop. It can handle all of Unity's supported languages (C#, JavaScript, and Boo). MonoDevelop has code completion support and other tools that make scripting more efficient.
You'll gain experience with MonoDevelop in the next section.
Scripting with C# in Unity
Let's get our feet wet by jumping right in and start scripting. The following are the steps to create a new script in Unity using C#:
Launch Unity.
Open the game project.
From the top menu, navigate and select Assets | Create | C# Script.
You'll see the script in the Project view and you'll be in script name edit mode. Name the script testScript
. We'll delete this script when we are finished:
The script is now visible in the Inspector view.
In the Project view, under Assets, double-click on the script. This will launch MonoDevelop and provide you with the ability to edit the script:
Add the following line of code in MonoDevelop in line 8:
Your script should now match the following screenshot:
In the MonoDevelop top menu, navigate and select File | Save or File | Save All.
Close the MonoDevelop window.
Review the script using the Inspector view to ensure that your changes were saved, as shown in the following screenshot:
Click on the Main...
Before we dive into scripting our game, we need to do a little planning. We will look at what we want to script and what events we want to trigger those scripts. We'll also determine what our data needs are. Since our game is relatively simple, this will not be a difficult set of tasks.
Let's review our game's design to determine what needs to be scripted. We know that the primary action a user will take is to navigate the player character in the game environment. As you've already seen, this has been taken care of for us by Unity. This functionality is already in place with no other programming required.
We'll need to plan for events regarding the player gathering corn and water and feeding the animals.
We've designed our game so that the player character can collect corn and feed it to the farm animals. The way we will accomplish this is to detect when the player character and the corn game objects collide. While that collision is taking place...
In this chapter, you learned about scripting with C# in Unity. You gained a cursory understanding of C# fundamentals including syntax and naming conventions. You were guided through the use of MonoDevelop so that you can edit scripts by using the code editor that comes with Unity. The chapter provided insights on how to create scripts and how to attach them to game objects. Using a hands-on approach, you were guided through the creation of your first C# script in our game.
In the next chapter, we'll add a graphic user interface to our game. We'll explore Unity 5's new UI system. We will create and script a full-screen navigational menu, a heads-up display, and a minimap. By the end of the next chapter, you will have created a professional-looking game with a full set of graphic user interface features.