Sharing Variables Across Scripts

Is it possible to share variables across scripts, and for that matter, across whole scenes?

You an access it from other scripts if it is a static variable for example:

playerScript.js
--------------------
static var Health : int = 100;

function OnGUI() {
    //Shows a gui label with health at the top left corner of the screen
    GUI.Label(Rect(10,10,100,25),"Health: " + Health);
}



enemyScript.js
-----------------------
var canAttack : boolean = true;

function Update() {
    //If the variable can attack is true
    if(canAttack) {

        //Start the function "doAttack" with the variable "(5)"
        doAttack(5);

        //Tells the script it can't attack
        canAttack = false;
    }
}

function doAttack(Damage : int) {
    //Loads the variable "Health" from the player script, and removes "Damage" from it.
    //PS. Damage is the number 5 we put in function update
    playerScript.Health -= Damage;

    //Waits in 1 second
    yield WaitForSeconds(1.0);

    //Tells the script it can attack again
    canAttack = true;
}

PS. You can also read the static variable if you want to, for example:

playerScript.js
--------------------
static var Health : int = 100;

function OnGUI() {
    //Shows a gui label with health at the top left corner of the screen
    GUI.Label(Rect(10,10,100,25),"Health: " + Health);
}



deathScript.js
-----------------------
function Update() {
    if(playerScript.Health =< 0) {
        beginDeath();
    }
}

function beginDeath() {
    //Start death animtion/sound or whatever

    //After 5 seconds . . .
    yield WaitForSeconds(5.0);

    //Respawn player? / Go to menu or whatever
}

2nd alternative:

You can use “GetComponent()” to get variables from a specific gameObject, for example:

playerScript.js
------------
var Health : int = 100;
-----------------------------------

otherScript.js
----------------------
Player(GameObject)
              |
             V
var Player : GameObject;

function Update() {
    if(Player.GetComponent(playerScript).Health <= 0) {
        //do something
    }
}

-------------------------------------------------------------

you can also get variables from other scripts on the same gameObject:

playerScript.js
---------------------
var Health : int = 100;


GUIscript.js
-----------------
function Update() {
    var HP : int = GetComponent(playerScript).Health;
}

function OnGUI() {
    GUI.Label(Rect(10,10,100,25), "Health: " + HP);
}

PS. “GetComponent()” can also be used for what it mainly is created for, access components on the gameObject such as:
ParticleEmitter, CharacterController etc.

Hope this helt you on the way :slight_smile:

// Alexander

1 Like

Alright. Does this work across scenes, as well?

Only if you tell the gameobject which holds the variable in it’s script not to be destroyed when changing scenes. For instance this may be useful if the script is the HUD.

You can create a variable in the script and add a prefab to it and read the variables from scripts on the prefab:

var prefab : GameObject;
var something;

function Start() {
    something = prefab.GetComponent(scriptname).variable;
}

PS. You can do as will said, add those lines at the top of the scriot, but i make a small edit:

function Awake() {
    DontDestroyOnLoad(this);
}

I use “this” instead (you can use both “this” or “gameObject”), to prevent the script from conflicting in some way and destroy components on the game object, since there is no need to get the GameObject from the transform on the GameObject. (sorry of my explanation is a bit fuzzy)

In short: You only need to directly get the gameobject, not from the transform.

ok i am having a problem lowering my players heath when a zombie is attacking heres my scripts can anyone help please

  1. DestroyOnHit

#pragma strict
#pragma implicit
#pragma downcast

class DestroyOnHit extends MonoBehaviour
{
public var hitsToDestroy : int = 1;
public var destructionParticles : GameObject;
public var destroyOnExplosion : boolean = true;
public var destroyParent : boolean = true;

function Start()
{
gameObject.layer = 11;
}

function Destruct()
{
if(destroyOnExplosion)
{
DestroyObject();
}
}

function Hit(hit : RaycastHit)
{
hitsToDestroy–;

if(hitsToDestroy <= 0)
{
DestroyObject();
}
}

function DestroyObject()
{
if(destructionParticles != null)
{
GameObject.Instantiate(destructionParticles, transform.position, Quaternion.identity);
}

if(destroyParent)
{
if(transform.parent != null)
{
Destroy(transform.parent.gameObject);
}
else
{
Destroy(gameObject);
}
}
else
{
Destroy(gameObject);
}
}
}

Following is the zombies script

var damageDone : int;

function OnTriggerEnter( other : Collider ) {
if ( other.GetComponent(DestroyOnHit) ) {
other.GetComponent(DestroyOnHit).hitsToDestroy += damageDone;
}
}

At the beginning of your code, put
[ code ]
but without the spaces.

At the end of a code segment, write
[ /code ]
also without the spaces.

Also.

Seriously? You’re adding damageDone to hitsToDestroy? How is that going to do damage?
Call the Hit function, that’s what it’s for. (as a side note, Hit has an argument which it never uses)