By using a little Unity magic, a script becomes a Component

While working in Unity, we wear the following two hats:

  • A Game-Creator hat
  • A Scripting (programmer) hat

When we first wear our Game-Creator hat, we will be developing our Scene, selecting GameObjects, and viewing Components; just about anything except writing our scripts.

When we put our Scripting hat on, our terminology changes as follows:

  • We're writing code in scripts using MonoDevelop
  • We're working with variables and methods

The magic happens when you put your Game-Creator hat back on and attach your script to a GameObject. Wave the magic wand — ZAP — the script file is now called a Component, and the public variables of the script are now the properties you can see and change in the Inspector panel.

A more technical look at the magic

A script is like a blueprint or a written description. In other words, it's just a single file in a folder on our hard drive. We can see it right there in the Projects panel. It can't do anything just sitting there. When we tell Unity to attach it to a GameObject, we haven't created another copy of the file, all we've done is tell Unity we want the behaviors described in our script to be a Component of the GameObject.

When we click on the Play button, Unity loads the GameObject into the computer's memory. Since the script is attached to a GameObject, Unity also has to make a place in the computer's memory to store a Component as part of the GameObject. The Component has the capabilities specified in the script (blueprint) we created.

Even more Unity magic

There's some more magic you need to be aware of. The scripts inherit from MonoBehaviour.

For beginners to Unity, studying C# inheritance isn't a subject you need to learn in any great detail, but you do need to know that each Unity script uses inheritance. We see the code in every script that will be attached to a GameObject. In LearningScript, the code is on line 4:

public class LearningScript : MonoBehaviour

The colon and the last word of that code means that the LearningScript class is inheriting behaviors from the MonoBehaviour class. This simply means that the MonoBehaviour class is making few of its variables and methods available to the LearningScript class. It's no coincidence that the variables and methods inherited look just like some of the code we saw in the Unity Scripting Reference.

The following are the two inherited behaviors in the LearningScript:

Line 9:: void Start ()

Line 14: void Update ()

Note

The magic is that you don't have to call these methods, Unity calls them automatically. So the code you place in these methods gets executed automatically.

Have a go hero – finding Start and Update in the Scripting Reference

Try a search on the Scripting Reference for Start and Update to learn when each method is called by Unity and how often.

Also search for MonoBehaviour. This will show you that since our script inherits from MonoBehaviour, we are able to use the Start() and Update() methods.

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

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