Overview of standard Unity classes

While I am not going to copy the whole Unity Script Reference into this chapter of the book, I would like to list a few important classes and functions that you are going to use quite often.

The most obvious and frequently used standard functions are Awake, Start, OnEnable, OnDisable, Update, and FixedUpdate:

  • Awake is the first function that is called when a scene is loaded. The Awake function can happen only once per scene load. It is generally a good idea to put all initialization code into the Awake function.
  • Start happens after Awake and also runs once. Sometimes it is a good idea to put some code into Awake and some other code into Start in order to make sure that one is executed after the other. When you have an Awake function in one script and another Awake function in another script, you cannot be sure which one will be executed first. If the order matters, put one of the pieces of code into the Start function.

    Tip

    There's another way to ensure a correct script execution order. You can decide which script is executed before or after by navigating to Edit | Project Settings | Script Execution Order from the main menu and then pressing the plus button in the Inspector, selecting the script whose execution order you would like to define, and then dragging it up or down in the interface. You will notice that the number on the right changes as you drag the script: this number is the script's execution order. You give the script its default execution order by removing it from this list. You can do this by pressing the minus button on the right from its name in the list.

  • The OnEnable function is a lot like Start, except it is called every time the object to which the script is attached is activated, as well as every time the component itself is enabled.
  • OnDisable is the opposite of OnEnable. It is called when the object or the component gets disabled.
  • Update is called every frame. Most of the games' logic usually happens here.
  • FixedUpdate is called on physics update, which is generally significantly more frequent than Update. All the code that cannot be frame rate dependent (such as, for instance, movement) should be in the FixedUpdate function.

Apart from the functions, there are whole classes with their methods and variables that you should absolutely know about when programming in Unity. It is hard to emphasize something in particular, and you really should just go through all the major classes listed in the Scripting Reference (http://docs.unity3d.com/Documentation/ScriptReference/). There are really a lot of classes, and there is hardly anyone who knows what all of them do, but we are going to go through a few that you should look at first thing: Mathf, Vector3, Color, Input, GameObject, Transform, Renderer, Material, Collision, Collider, and of course Object, Behaviour, Component, and MonoBehaviour.

The last four classes are especially important, since they include things that you are going to use every time you write a script in Unity, including functions like Update, Destroy, GetComponent, and Start, as well as variables such as enabled, name, layer, and tag. Try to read descriptions of these classes and their functions and variables very carefully, and look at the basic examples the documentation offers with them.

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

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