Reader small image

You're reading from  Getting Started with Unity 5

Product typeBook
Published inMay 2015
Publisher
ISBN-139781784398316
Edition1st Edition
Tools
Right arrow
Author (1)
Dr. Edward Lavieri
Dr. Edward Lavieri
author image
Dr. Edward Lavieri

Dr. Edward Lavieri is a veteran software engineer and developer with a strong academic background. He earned a Doctorate of Computer Science from Colorado Technical University, an MS in Management Information Systems (Bowie State University), an MS in Education (Capella University), and an MS in Operations Management (University of Arkansas). He has been creating and teaching computer science courses since 2002. Edward retired from the U.S. Navy as a Command Master Chief after 25 years of active service. He is the founder and creative director of three19, a software design and development studio. Edward has authored more than a dozen technology books, including several on Java.
Read more about Dr. Edward Lavieri

Right arrow

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# programming primer


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...

Using MonoDevelop


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#:

  1. Launch Unity.

  2. Open the game project.

  3. From the top menu, navigate and select Assets | Create | C# Script.

  4. 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.

  5. 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:

  6. Add the following line of code in MonoDevelop in line 8:

    print("Hello Little Farmer Colt!");
  7. Your script should now match the following screenshot:

  8. In the MonoDevelop top menu, navigate and select File | Save or File | Save All.

  9. Close the MonoDevelop window.

  10. Review the script using the Inspector view to ensure that your changes were saved, as shown in the following screenshot:

  11. Click on the Main...

Scripting our game


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.

Script planning

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.

Gathering corn

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...

Summary


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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Unity 5
Published in: May 2015Publisher: ISBN-13: 9781784398316
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dr. Edward Lavieri

Dr. Edward Lavieri is a veteran software engineer and developer with a strong academic background. He earned a Doctorate of Computer Science from Colorado Technical University, an MS in Management Information Systems (Bowie State University), an MS in Education (Capella University), and an MS in Operations Management (University of Arkansas). He has been creating and teaching computer science courses since 2002. Edward retired from the U.S. Navy as a Command Master Chief after 25 years of active service. He is the founder and creative director of three19, a software design and development studio. Edward has authored more than a dozen technology books, including several on Java.
Read more about Dr. Edward Lavieri