Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning C# by Developing Games with Unity 5.x

You're reading from   Learning C# by Developing Games with Unity 5.x Develop your first interactive 2D platformer game by learning the fundamentals of C#

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher Packt
ISBN-13 9781785287596
Length 230 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready 2. Introducing the Building Blocks for Unity Scripts FREE CHAPTER 3. Getting into the Details of Variables 4. Getting into the Details of Methods 5. Lists, Arrays, and Dictionaries 6. Loops 7. Object, a Container with Variables and Methods 8. Let's Make a Game! – From Idea to Development 9. Starting Your First Game 10. Writing GameManager 11. The Game Level 12. The User Interface 13. Collectables — What Next? Index

Introducing the MonoDevelop code editor

Unity uses an external editor to edit its C# scripts. Even though it can create a basic starter C# script for us, we still have to edit the script using the MonoDevelop code editor that's included with Unity.

Syncing C# files between MonoDevelop and Unity

Since Unity and MonoDevelop are separate applications, Unity will keep MonoDevelop synchronized with itself. This means that if you add, delete, or change a script file in one application, the other application will reflect the changes automatically.

Opening LearningScript in MonoDevelop

Unity will synchronize with MonoDevelop the first time you tell it to open a file for editing. The simplest way to do this is by double-clicking on LearningScript in the Scripts folder. It might take a few seconds for MonoDevelop to open and sync.

Our window should look like this:

Opening LearningScript in MonoDevelop

MonoDevelop launched with LearningScript open, and ready to edit.

What we see now is a default C# script structure that Unity creates. It contains information on what namespaces are used in the script, the class definition, and two methods that Unity adds by default, as shown here:

Opening LearningScript in MonoDevelop

The namespace – highlighted in blue

The namespace is simply an organization construct. It helps organize parts of code. Don't worry too much about them now. We won't need to create them anytime soon. All we will need to know for now is how many namespaces we are using in our script.

In our script, we can see these two lines:

using UnityEngine;
using System.Collections;

The preceding two lines simply mean that our script will be using the UnityEngine and System.Collections namespaces and we will have access to all parts of these libraries. These two namespaces are added to any new C# script by default, and we will use them in most of our cases.

The class definition – highlighted in green

A class definition starts with the class keyword, followed by a class name and an optional base class name, followed by a class body enclosed in curly braces:

public class LearningScript : MonoBehaviour {

}

Tip

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  • Log in or register to our website using your e-mail address and password.
  • Hover the mouse pointer on the SUPPORT tab at the top.
  • Click on Code Downloads & Errata.
  • Enter the name of the book in the Search box.
  • Select the book for which you're looking to download the code files.
  • Choose from the drop-down menu where you purchased this book from.
  • Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

Again, don't worry about this too much. Let's not introduce too much theory. All that we need to focus on now is how the class definition looks.

Note

The code contained inside your class is called class body. By default, Unity creates two functions inside a class body.

Watching for possible gotchas while creating script files in Unity

Notice line 4 in the preceding screenshot:

public class LearningScript : MonoBehaviour

The class name LearningScript is the same as the filename LearningScript.cs. This is a requirement. You probably don't know what a class is yet, but that's okay. Just remember that the filename and the class name must be the same.

When you create a C# script file in Unity, the filename in the Project tab is in Edit mode, ready to be changed. Please rename it right then and there. If you rename the script later, the filename and the class name won't match. The filename would change, but line 4 will be this:

public class NewBehaviourScript : MonoBehaviour

This can easily be fixed in MonoDevelop by changing NewBehaviourScript in line 4 to the same name as the filename, but it's much simpler to do the renaming in Unity immediately.

Fixing synchronization if it isn't working properly

What happens when Murphy's Law strikes and syncing just doesn't seem to be working correctly? Should the two apps somehow get out of sync as you switch back and forth between them for whatever reason, do this. Right-click on Unity's Project window and select Sync MonoDevelop Project. MonoDevelop will resync with Unity.

Adding our script to GameObject

We have created the LearningScript class. Its code is saved in the file in the Project/Assets folder. To include an instance of this class in our project, we will add it as a component to an empty GameObject.

Lets create a new GameObject. In the menu, navigate to GameObject | Create Empty Child, as shown here:

Adding our script to GameObject

There are a number of ways of adding our LearningScript component to GameObject. Let's talk about the simplest one:

  1. Select your newly created GameObject.
    Adding our script to GameObject
  2. Drag and drop the Script file from the Project tab to the empty space underneath the Transform component.
    Adding our script to GameObject

We can now see that our LearningScript file has been added as a component to the GameObject. This means that an instance of LearningScript is active and ready to execute code.

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image