Scripts and MonoDevelop

Click on GameObject | Create Empty and a new GameObject will appear on the stage with the name GameObject. It should already be highlighted, so click it once more and the name will become editable, call it AdvanceToNextLevel.

We're going to create and attach a C# sharp script to the AdvanceToNextLevel GameObject that will load a new scene as soon as this one is initialized. Right-click on the Scripts folder you created earlier and then click on Create and select the C# Script label. Once you click on it, a script will appear in the Scripts folder and it should already have focus and be asking you to type a name for the script, call it AdvanceToNextLevel. Double-click on the script in Unity and it will open MonoDevelop, which is an open source, integrated development environment that runs on Linux, Mac OSX, and Windows. It supports automatic code completion, source control, and Graphical User Interface (GUI).

After MonoDevelop has loaded, you will be presented with the C# stub code that was created automatically for you by Unity when you created the C# script.

Let's break down what's currently there before we replace some of it with new code. At the top you will see the following two lines of code:

using UnityEngine;
using System.Collections;

The UnityEngine namespace contains interfaces and class definitions that let MonoDevelop know about all the addressable objects inside Unity.

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables, and dictionaries. We won't be using the System.Collections in this script, so we can go ahead and delete that line although it won't affect if you leave it in.

The next line of code you'll see is as follows:

public class AdvanceToNextLevel : MonoBehaviour {

The class name must match the filename. When Unity created our C# stub code, it took care of this, we can see this result as our file is named AdvanceToNextLevel and the class is also named AdvanceToNextLevel. Next up is the : MonoBehaviour section of the code. All behavior scripts must inherit from : MonoBehaviour directly or indirectly. While this happens automatically in JavaScript, it must be explicitly declared inside the C# scripts. If you create your script inside Unity through the Create | C# Script, the created template will already contain the necessary definition.

The line after that is a method definition for a method named Start, as shown in the following code. It isn't a user method, but one that belongs to MonoBehaviour.

void Start () {
  
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased 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.

Start is called just before any of the Update methods are called for the first time. We're not going to use Start; we're going to use Awake instead, so you can change the word Start to Awake as the method signatures are the same. This is the method that will load our next scene.

Start is only called once in the lifetime of the behavior. The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code until it is really needed. Awake is always called before any Start method functions. This allows you to order the initialization of your scripts.

Inside the curly braces add the following line to the code:

Application.LoadLevel("TitleScreen");

LoadLevel will simply destroy everything in the current scene, excluding GameObjects that have DontDestroyOnLoad set, and load the scene specified as a string. The string is the filename of the scene, in this case TitleScreen, which we created earlier.

The last piece of code is the Update method. This method is called once every frame so it's an ideal place to add any code that will need to execute constantly such as checks for movement. This particular script has no need for the method, so go ahead and delete it.

Your final script should look like the following code:

usingUnityEngine;

public class AdvanceToNextLevel : MonoBehaviour {

  // Use this for initialization
  void Awake () {
    Application.LoadLevel("TitleScreen");
  }
}

Now we have finished our first script, we need to attach it to a GameObject in our scene. Go back to Unity and you'll see a small activity indicator in the lower-right of the screen, that's your script being compiled and checked for errors by Unity. If there are ever any issues, they will appear in the console. Drag your AdvanceToNextLevel script to your AdvanceToNextLevel GameObject.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85