Variables! — The building blocks of programming

by Lance Gold

Development allows a connection between assets and actions

Return to index

Create a Scripts folder to contain all Project Scripts

Create a Scripts folder to contain all Project Scripts

After creating a folder in the Project frame to hold C-sharp scripts, right click on the folder to create a new C-sharp script. It is important to name the Script correctly while it is newly highlighted. This is because the Script name needs to match the class name as displayed in the Inspector.

The script name must match the class name in C-sharp

The script name must match the class name in C-sharp

Click to remove the highlight of the Script name in the Project pane. Double-click to open Microsoft Visual Studio. Double-click again to view the script in Visual Studio.

Viewing script in Visual Studio

Viewing script in Visual Studio

Verify that the class name matches the Script name.

Next attach the script in the Project frame to the Game object in the Hierarchy.

Either:

(a) click ‘Player’ in Script folder and drag to ‘Player’ in Hierarchy,

or

(b) click and highlight ‘Player’ in Hierarchy and then click and drag ‘Player’ script into the ‘Add Component’ space of the Inspector.

Player script listed in Inspector of Player Game object

Player script listed in Inspector of Player Game object

Now save project and start coding.

Code a starting position for an object.

Initial Position

Identify a component. Here the class is Player, same as in the Hierarchy. The component is Transform, as listed in the Inspector.

Matching class and component names to Hierarchy and Inspector

Matching class and component names to Hierarchy and Inspector


public class Player : MonoBehaviour
{
      ...
        //take the current position set to position (0,0,0)
        transform.position = new Vector3(0, 0, 0);
      ...
}

Change Position

The Open Reference tool tip from the ‘?’ icon

The Open Reference tool tip from the ‘?’ icon

The help ‘?’ icon opens the Unity manual at the Transform chapter of the manual. To search methods, click from ‘Manual’ to ‘Scripting API’.

Top result after search for Transform API

Top result after search for Transform API

“Position, rotation and scale…” is the search choice. ‘Translate’ is the desired method which can move the transform.

The ‘Translate’ method moves the transform

The ‘Translate’ method moves the transform


    void Update()
    {
        transform.Translate(Vector3.right);
        // or also transform.Translate(1,0,0);
        // one unit is one meter, at frame rate
    }

Save Visual Studio updated file before running the Unity project

Movement Speed


    void Update()
    {
        transform.Translate(Vector3.right * Time.deltaTime);
    }

deltaTime is the time elapsed since the previous Unity update cycle, so if there was 1/60th second delta’d from the prior cycle, deltaTime = float 0.0166something.

No hard coded Unity component values Create variables to hold component values in the C-sharp script.


public class Player : MonoBehaviour
{
    // speed variable for Player movement
    // declare variables outside of Unity loops
    public float speed = 5f;
...
    void Update()
    {
        transform.Translate(Vector3.right * speed * Time.deltaTime);
        // instead of using ‘Vector3.right * 5 * Time.deltaTime‘
    }
}

‘speed’ is declared inside of the ‘Player’ class, but outside of either ‘Start()’ or ‘Update()’ or any other methods. It can be either ‘public’ or ‘private’. A variable declared inside a method is ‘local’ to the method and does not have a ‘public’ or ‘private’.

When declared ‘public’, the variable is viewed as a script component for adjustment in the Unity Inspector.

View public script variables in the Unity Inspector

View public script variables in the Unity Inspector

Click the 3-dot icon to choose ‘Reset’ to change value to what is initiated in the script code.

When declared ‘private’, the component disappears from the Inspector. Also, it is helpful to prefix an underscore ‘_’ for private variables.


    private float _speed = 5f;
...

    transform.Translate(Vector3.right * _speed * Time.deltaTime);

Use ‘[SerializeField]’ to allow private variables to be manipulated in the Inspector, but not available to other objects outside the class


    [SerializeField]    // no semicolon ‘;’
    private float _speed = 5f;