Time for action – Creating and organizing scripts

Creating a new C# script in Unity is similar to our approach for creating scripts using JavaScript or any of the other scripting languages.

  1. Select Assets | Create | C Sharp Script. This will create a new C# script in the project:

    Note

    While the Boo Script does appear as an option, it is not usable for Unity iOS projects.

    Time for action – Creating and organizing scripts
  2. Once you have created the new script it will appear in the Project view with the name NewBehaviorScript with some stubs for the methods Start() and Update():
    Time for action – Creating and organizing scripts
  3. Create a folder named Scripts and then drag the newly created script into the Scripts directory.

What just happened?

A best practice for scripts is to put them in a folder in the project. What you call this folder doesn't really matter, as it is more for your own organizational purposes than it is for Unity. You may choose to put UI centric scripts in one directory and AI scripts in another. However you manage the scripts is up to you, but you will create a large number of scripts in the course of creating the average game so you want to organize them so that they aren't all sitting at the root of your game project.

Attaching scripts to Game Objects

As mentioned earlier, you can add a script to a GameObject by simply dragging the script onto the game object itself. What is important here, is that the GameObject is the owner of the script, if that game object is not participating in the scene – the methods in the script will never be called. For scripts that are global in nature, a common practice is to create an Empty Game Object in the scene and attach the global scripts to it. While it is common for people to associate these global scripts with the camera, you want to avoid this as many complex games will have many cameras – complicating the activation of the scripts as well as determining which object in the scene owns them.

Exposing variables in the Unity editor

Quite often you will find yourself in the position of having written a script providing some values for variables within your script. As you test your game you will find that you will want to expose some of these variables to the Unity Editor so that you can modify them in the editor without having to edit and recompile the scripts themselves.

This is particularly important since many times you will want to provide a default value within the script which you can override due to some specific game use case. Rather than coming up with some complex mechanism to do this, simply declare the variable as a public variable and it will show up in the Unity editor.

using UnityEngine;
using System.Collections;

public class SomeScript : MonoBehaviour {

  public int testExpose = 5;
  public String testStringExpose = "Test";
  
  
  // Update is called once per frame
  void Update () {
  
  }
}

Key scripting methods

Scripting inside Unity consists of attaching custom script objects called behaviors to game objects. Different functions inside the script objects are called on certain events. The most used ones being the following:

Method

Purpose

Awake

Called when the script object is being loaded but after all Game Objects have been initialized

Start

Called only one time – when the object this script is attached to is initially used in the scene

Update

This function is called before rendering a frame. This is where most game behavior code goes, except physics code.

FixedUpdate

This function is called once every physics time step. This is the place to do physics-based game behavior. Unlike Update, this method is not called every time, but at a specific interval.

Use this for things that should be frame rate independent.

LateUpdate

This function is called after Update has been called. This is useful if you want to execute some functionality after all of the other objects in the scene have been updated.

"Other Methods "

Code outside functions is run when the object is loaded. This can be used to initialize the state of the script.

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

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