Methods in C# | Unity Tutorial

Games are all about action. To give your game objects custom behavior, you can use methods. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

A method in a script performs an operation or function. In this tutorial, we will continue with the example in our previous post to show the two methods that appear in any C# script you create in Unity: Start and Update.

The Start Method

In the last tutorial, we deleted the methods from the Cube script to prevent confusion. Recreate the Start method with the following code.

Note that typing // in front of a line of code turns the code into a comment, which is not read by the compiler.

public class Cube : MonoBehaviour {

public float sizeModifier = 2.5f;
public string newName = "Cubey1337";
public bool isRotated = false;

// Use this for initialization
void Start () {

}
}

In the preceding code, the method name is Start. Each word in the name of a method is capitalized. The Start method is executed when the game is run.

A method can perform an operation and return the result. For instance, a method can add numbers, return the sum, and store the sum in a variable. void means the Start method does not return anything. The method can still execute operations.

In between the parentheses, you can pass any parameters of a method. Parameters make a method more specific. For instance, a method that changes the TV channel could have the number of the new channel as a parameter.

The method’s definition goes between the curly braces. The code within the braces will be executed when the Start method is called.

Printing a Message

Suppose you want to print a message in the console. Type the following code to print “Hello World!” when the Start method is called.

// Use this for initialization
void Start () {
Debug.Log ("Hello world!");
}

To call a method, you call its class, use the dot operator, type the method name, and pass the parameters in parentheses. Debug is a class that is defined in the Unity engine. The . operator accesses a method inside the Debug class. Log calls the Log method. We passed the string “Hello world!” as a parameter.

Save the script, and open Unity. To call the Start method, press Play. Open the Console. As evident in the following image, the message “Hello world!” will print in the Console.

Stop running the scene. In Cube.cs, change “Hello world!” to “Started!”.

The Update Method

The other method we previously deleted is Update. Declare the Update method with the following format.

// Use this for initialization
void Start () {
Debug.Log ("Hello world!");
}

// Called once per frame.
void Update () {

}

When a game or app runs, the smartphone or computer on which it is running updates the app constantly: processing, performing calculations, drawing pixels, sending messages, and more. The Update method executes every time a game is updated. An app running at 60 frames per second calls the Update method 60 times each second.

Type the following code into the Update method, which will print “Updating!” every time the Update method executes.

// Called once per frame.
void Update () {
Debug.Log (Updating!);
}

Save the script, and open Unity. When the compilation completes, press Play. “Started!” and “Updating!” will appear in the Console. “Updating!” will print multiple times as the Update method is called.

Select the Collapse tab from the Console. The Collapse window collapses identical messages that are printed one after the other. As evident in the next screenshot, the number to the right of “Updating!” increase every time the Update method is called.

Stop playing the scene. Let’s use the variables sizeModifier and newName in the Start and Update methods. In Cube.cs, replace the Log line in the Start method with the following code.

// Use this for initialization
void Start () {
transform.localScale *= sizeModifier
}

transform accesses the Transform component of the Cube game object. . accesses something in the Transform component, in this case the localScale property. localScale stores the scale of the cube, which is currently 1 1 1, as you can see in the Inspector.

*= sizeModifier takes the values stored in transform.localScale and multiplies them by sizeModifier, which is 2.5. In other words, the Scale values will multiply by 2.5.

Save the script, and open Unity. Press Play. Cube will be bigger than it was before. To change Cube’s size, you can change the Size Modifier property in the Cube (Script) component. For instance, if you change the value of Size Modifier to 0.1, Cube’s size will decrease.

Want to learn more about methods? Check out our Unity3D course, where you build 30 virtual reality games!

 

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com