Using Unity's Update and Start methods

Every time you create a script in Unity, these two skeleton methods are included. That's because they are rather important. These are the most commonly used MonoBehaviour methods, see the next screenshot for others. I like to call these Unity's magic methods because you don't call these methods, Unity does. It's usually important that at least one MonoBehaviour method is included in a Unity script to cause the script to execute. I say usually because other methods in the script may be called from another script or class.

How do I know these two methods are called by Unity and that they are MonoBehaviour methods? Here, the Unity Scripting Reference is your friend.

Here's just a portion of the methods Unity can call in a script. This is from the Scripting Reference. Just search for MonoBehavior:

Using Unity's Update and Start methods

Look at line 4 of LearningScript:

public class LearningScript : MonoBehaviour

This line says that LearningScript inherits from MonoBehaviour. Any script that inherits from MonoBehaviour will automatically call the methods Update() and Start() if they are in the script. Therefore, if you want, you can create a script in MonoDevelop instead of Unity, just have it inherit from MonoBehavior by adding:: MonoBehaviour after the class name.

Note

Please notice the colon that needs to be included.

The Start method is called one time

Unity calls this method only one time. When the GameObject your script is attached to is first used in your scene, the Start() method is called. This method is primarily used to initialize, or setup, the member variables in your script. This allows everything in your script to be ready to go before Update() is called for the first time.

You've probably noticed that many of the examples I used in LearningScript are making use of Start(). These examples weren't initializing any code, I was just taking advantage of the fact that since Start() is only called once, displaying output to the Console would, therefore, only be displayed once, which just made it easier to see the output displayed.

The Update method is called over and over and over…

As you study the sample code in the Scripting Reference, you will notice that a vast majority of the code is in the Update() method. As your game runs, the Scene is displayed many times per second. This is called Frames per Second, or FPS. After each frame is displayed, the Update() method is called by Unity to run your code.

Since Update() is called every frame, it allows your game to detect input, such as mouse clicks and key presses, every frame. User input is one of the topics we are about to cover in the next chapter.

Pop quiz – understanding method operation

Q1. What are the minimum requirements for defining a method?

Q2. What is the purpose of the parentheses at the end of the method's name?

Q3. What does void mean in a method definition?

Q4. In a Unity script, how is the Update() called?

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

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