Update :: GameDev Unity3D cookbook

“Update” is a function used in Unity3D, which refer to: “The need to provide for certain game objects, execution loops.”

For example, when creating a character in a game, the programming of this character may have a number of functions that should be checked, and executed in every single frame of the game.

Different game engines have different names, and ways to implement these procedures. Here we’ll see how it works on Unity3D.

Create a scene in Unity, and add two cubes to it, and name them as “Cubo1” and “Cubo2″.

[code lang=”javascript”]
private var teste:int = 0;

function Update () {
Debug.Log(“Frame : “+teste);
teste++;
}
[/code]
Obs.: Create this script, name it as you wish. Drag the script, from the “Project” panel, to one of the game objects in Hierarchy panel (prefer one of the cubes). As soon as you run the game, you will see the output in the Console window.

This script, counts the number of the frames in your game. This code, increments in 1, the value in teste variable. In the first frame it is zero, than it is incremented to one; and so on.

Now, lets make the cube to move:

[code lang=”javascript”]
private var teste:int = 0;

function Update () {

transform.Translate(Vector3.right * Time.deltaTime);
Debug.Log(“Frame: “+teste);
teste++;
}
[/code]

Now, the script makes the cube moves across the X axis.
Take a look at the sample here.

Leave a Reply

Your email address will not be published. Required fields are marked *