Accessing a variable of a script from another scene.

Ok so what i have is 2 seperate scenes. The actual “game scene” where you kill things and pickup points. I have it so that once you have picked up all the points in that scene you will go to the Shop Menu through Application.LoadLevel. In the shop menu right now there is 1 button which i am trying to make it so that the variable of the “shoot” script in the “game scene” that determines how far you shoot
will be increased if you have enough points. I also want that increase to stay on future levels like an upgrade. The errors I am getting with the ShopMenu script is

Assets/Scripts/ShopMenu.js(15,34): BCE0020: An instance of type ‘Points’ is required to access non static member ‘score’.

Assets/Scripts/ShopMenu.js(17,35): BCE0020: An instance of type ‘shoot’ is required to access non static member ‘gunRange’.

My Shop Menu script is

function OnGUI () {
	GUI.Box (Rect (10,10,250,90), "Shop Menu");
    
	if (GUI.Button (Rect (20,40,200,20), "Longer Range:   15 Points  ")) {
    
		Debug.Log ("Bought Longer Range");
    
		BuyRange();
	}
}

function BuyRange() {

	var points = gameObject.Find("Player").GetComponent(Points);
    
	var pointsscore = Points.score;
    
	var shootrange = gameObject.Find("TankTurret").GetComponent(shoot);
    
	var shootingrange = shoot.gunRange;
    
		if (pointsscore >= 15) {
    
		shootrange.gunRange = 50;
	}
}

The shoot script is
var gunRange = 40;
var bullet : Rigidbody;
function Update () {
	if (Input.GetKeyDown ("space"))
	 {
		var clone : Rigidbody;
		clone = Instantiate(bullet, transform.position, transform.rotation);
	//Give the cloned bullet velocity along z axis
	clone.velocity = transform.TransformDirection (Vector3.up * gunRange);

	}
} 

The Points script
 var score = 0;

function OnTriggerEnter ( other : Collider ) {
	if (other.tag == "Points") {
			score += 5;
			Destroy (other.gameObject);
	}
}

function OnGUI (){
	GUI.color = Color.red;
	GUI.Box (Rect(25,25,100,30), "Points: "+score);
}

function Update () {
	
	if (score==15) {
		loadShopMenu();
	}
}

function loadShopMenu ()
{

	yield WaitForSeconds(3);
		Application.LoadLevel(2);
} 

I’m using static vars to hold things like points, lives, health etc. These variables are defined in a script called Control.js, and they may be accessed as Control.points, Control.health etc. This script is attached to my player (a FPS controller), and their static variables survive during the whole game:

static var gunRange: float = 100;

Another alternative is to keep these variables in a script assigned to an empty object, and include the DontDestroyOnLoad instruction in its Awake function:

var gunRange: float = 100;

function Awake () {
    DontDestroyOnLoad (transform.gameObject);
}

EDITED: If you want to use the static variable alternative, your scripts should look like below:

The ShopMenu.js script:

function OnGUI () { 
	
	GUI.Box (Rect (10,10,250,90), "Shop Menu");
	if (GUI.Button (Rect (20,40,200,20), "Longer Range:   15 Points  ")) {
		Debug.Log ("Bought Longer Range");
		BuyRange();
	}
}

function BuyRange() {

	if (Points.score>=15){ // if the player has enough points...
		shoot.gunRange = 50;  // buy the new range...
		Points.score -= 15;  // and subtract the points spent
	}
}

The shoot.js script:

var bullet : Rigidbody;
static var gunRange = 40;  // this variable may be accessed anywhere as shoot.gunRange

function Update () {

	if (Input.GetKeyDown ("space")) {
		var clone : Rigidbody;
		clone = Instantiate(bullet, transform.position, transform.rotation);
		//Give the cloned bullet velocity along z axis
		//(is it correct? the bullet will go the Up direction of the turret!)
		clone.velocity = transform.TransformDirection (Vector3.up * gunRange);
	}
}

The Points.js script:

    static var score = 0;  // this variable may be accessed as Points.score anywhere
    
    function OnTriggerEnter ( other : Collider ) { 
    
    	if (other.tag == "Points") { 
    		score += 5; 
    		Destroy (other.gameObject); 
    	} 
    }
    
    function OnGUI (){ 
    
    	GUI.color = Color.red; GUI.Box (Rect(25,25,100,30), "Points: "+score); 
    }
    
    function Update () {
    
    	if (score==15) {
    		loadShopMenu();
    	}
    }

    function loadShopMenu () {

        yield WaitForSeconds(3);
        Application.LoadLevel(2);
    }

if(points = 100) {
uprange up = (Range).GetComponent(“Range”);
eh.uprange(+10);
}

and in your ange class you should have

uprange + range = currange

sorry im very new to coding and unity. What would my range class be. and im not sure how to read psuedocode. :stuck_out_tongue:

ok so i need to change the code in shop menu
from if (pointsscore >= 15) {
to
if (pointsscore >=15) {
uprange up = (Range).GetComponent(“Range”).uprange(+10); ?
i really am not sure how to code the rest of what you said to do…

ahh well are you using java or C#?

It will take around 20 - 30 minutes and i will have skype downloaded.

What if what we need is to acces a NON static variable from another JAVASCRIPT ?

TRAP’S SCRIPT (FISICAMinaBARRERA)

var VidaMinaB : int = 20 ;

function Update() { if (VidaMinaB <= 0) { Instantiate (ExplosioMina, transform.position , Quaternion.identity); Destroy(this.gameObject);
} }

(then this value is changing since it is the trap’s health (its destructible), I made a script which instances a damage area, and I want this gamobject (damage area) knows when the trap’s healt is 0 in order to destroy it… ; but if i set the variable VidaMinaB as static, then , the damage area script doesnt receive the new value of VidaMinaB, and if the trap is desroyed the damage area remains there…)

im calling the variable in the other script like this :

DAMAGE AREA SCRIPT (FÍSICABARRERAMina)

var VIDAMINAB : int ;

function Update () { VIDAMINAB = FISICAMinaBARRERA.VidaMinaB;

if (VIDAMINAB <= 0) { Destroy(this.gameObject , 0.5); } }

I sopose there is some way to access this “int” that is NON STATIC of antoher JAVASCRIPT … if i set the variable nonstatic i only get the Error :

  • An instance of type “FISICAMinaBARRERA” is required to access non static member “VidaMinaB” -

And finally, the other problem is: when I destroy 1 trap, all the other traps are destroyed too… , I watch the value VIDAMINAB of the damage areas created and is still 20, even when the traps are destroyed… this i sbecause it is static… that’s why im looking for the way to access this variable but it must be NONstatic in order to receive the new values… or am I wrong?

I’d higly appreciate your help, thank you very much in advance.

Have a nice day

Angel M.