Time for action – writing a function
In this tutorial, you'll begin writing your own functions and calling them to be executed. Perform the following steps to write a function:
Create a new script in the same way you created the color-changing script, but name it
ObjectMover
. Open the script and add the function declaration namedMoveObject
below the declaration forUpdate
, so that your full code file appears as shown in the following code:using UnityEngine; using System.Collections; public class ObjectMover : MonoBehaviour { //use this for initialization void Start () { } //Update is called once per frame void Update () { } void MoveObject () { } }
You've now declared a brand-new function, as simple as that.
Next, add the following line of code that will make it move positively along the x axis (to the right) by two units:
Void MoveObject () { gameObject.transform.Translate(2.0f, 0.0f, 0.0f); }
The parentheses at the end of this line tell us that a function is...