Overview: Member Variables & Global Variables Manual     Reference     Scripting  
Scripting
Overview: Member Variables & Global Variables

Any variable defined outside of any function defines a member variable. The variables are accessible through the inspector inside Unity. Any value stored in a member variable is also automatically saved with the project.

JavaScript
var memberVariable = 0.0; 

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public float memberVariable = 0.0F;
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public memberVariable as single = 0.0F

The above variable will show up as a numeric property called "Member Variable" in the inspector.

If you set the type of a variable to a component type (i.e. Transform, Rigidbody, Collider, any script name, etc.) then you can set them by dragging game objects onto the value in the inspector.

JavaScript
var enemy : Transform;

function Update() {
if ( Vector3.Distance( enemy.position, transform.position ) < 10 )
print("I sense the enemy is near!");
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Transform enemy;
void Update() {
if (Vector3.Distance(enemy.position, transform.position) < 10)
print("I sense the enemy is near!");

}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public enemy as Transform

def Update():
if Vector3.Distance(enemy.position, transform.position) < 10:
print('I sense the enemy is near!')

You can also create private member variables. Private member variables are useful for storing state that should not be visible outside the script. Private member variables are not saved to disk and are not editable in the inspector. They are visible in the inspector when it is set to debug mode. This allows you to use private variables like a real time updating debugger.

JavaScript
private var lastCollider : Collider;

function OnCollisionEnter(collisionInfo : Collision ) {
lastCollider = collisionInfo.collider;
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
private Collider lastCollider;
void OnCollisionEnter(Collision collisionInfo) {
lastCollider = collisionInfo.collider;
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

private lastCollider as Collider

def OnCollisionEnter(collisionInfo as Collision):
lastCollider = collisionInfo.collider

Global variables

You can also create global variables using the static keyword.

This creates a global variable called someGlobal.

JavaScript
// The static variable in a script named 'TheScriptName.js'
static var someGlobal = 5;

// You can access it from inside the script like normal variables:
print(someGlobal);
someGlobal = 1;

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public static int someGlobal = 5;
void Example() {
print(someGlobal);
someGlobal = 1;
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public static someGlobal as int = 5

def Example():
print(someGlobal)
someGlobal = 1

To access it from another script you need to use the name of the script followed by a dot and the global variable name.print(TheScriptName.someGlobal);
TheScriptName.someGlobal = 10;