x


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);

}

more ▼

asked Jul 09 '11 at 04:04 PM

Airmand gravatar image

Airmand
46 3 6 7

sorry i dont know how to put code in properly i tried the greater pre lesser at the beginning of the scripts and the greater code lesser at the end of the scripts but that didnt work

Jul 09 '11 at 04:27 PM Airmand
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

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);
    }
more ▼

answered Jul 09 '11 at 04:23 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

oh ok i didnt quite know what static variable did. I changed the gunRange and points variables to static variables and fixed some of the code that was wrong in Shop Menu but now i get NullReferenceException ShopMenu.BuyRange () (at Assets/Scripts/ShopMenu.js:14) ShopMenu.OnGUI () (at Assets/Scripts/ShopMenu.js:9)

Jul 09 '11 at 04:31 PM Airmand

A variable declared outside any function with var or public var is a public variable. It has script scope, what means that it's known by everybody in that script, but is a complete strange for other scripts. Static variables, on the other hand, have program scope, which means they are known by every script in the game. They also live during the whole game, even when a new level is loaded.
But there is another very important difference: a static variable is unique, while a public variable exists in each instance of the script where it's defined. If you have a variable defined as var ammo in a weapon script, for instance, and you have two of these weapons in scene, each one will have its own instance of ammo. If you decrement ammo in one weapon, the ammo instance of the other weapon will not be altered. They are like the Tornado Twins: if you kill one of them, the other will continue creating tutorials!
If that ammo variable were declared as static var ammo, on the other hand, both weapons would share the same ammo: if you decremented it in one weapon, the other would watch its ammo getting down too.
Well, that's the difference between static and public variables. I'm checking your code and will be back soon!

Jul 09 '11 at 05:54 PM aldonaletto

Edited the answer to show how to use the static variables in your scripts. Remember that static variables are unique. If you have more than one TankTurret object, all of them will share the same gunRange, since it's a static variable now.

Jul 09 '11 at 06:25 PM aldonaletto

im sorry i didnt even see the comments you were leaving. I believe the scripts you edited of mine work now. I just have to create a level 3 and see if the gunRange increased after it was bought

Jul 09 '11 at 07:57 PM Airmand

and thank you for the explanation. it is much clearer now

Jul 09 '11 at 07:59 PM Airmand
(comments are locked)
10|3000 characters needed characters left

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

and in your ange class you should have

uprange + range = currange

more ▼

answered Jul 09 '11 at 04:44 PM

Babilinski gravatar image

Babilinski
119 40 42 43

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 09 '11 at 05:07 PM

Airmand gravatar image

Airmand
46 3 6 7

if(points = 100) // your if block............................................................................................................ uprange up = (Range).GetComponent("Range")// replace "Range" with the code name that has the range in it........................................................................................................................................................ eh.uprange(+10); // this adjusts the value of uprange..........................................................................

in the script where you have the range int. you should have uprange + range = cur.range;

Jul 09 '11 at 05:20 PM Babilinski

i dont understand what uprange is

Jul 09 '11 at 05:32 PM Airmand

its a variable that i made up . exp.

your range is 5 then you go to the shop and you update your range buy 3 so

3 + 5 = currange AKA curent range

Jul 09 '11 at 05:37 PM Babilinski

im sorry i just really dont know how to read your pseudocode. What is cur.range supposed to be?

Jul 09 '11 at 05:47 PM Airmand

okay currange = a value that will change as the character gets more range. uprange = a value that will be the amount of range added to your currange after you have enough points range = a value that is the starting value of range when you start the game;

Jul 09 '11 at 05:53 PM Babilinski
(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Jul 09 '11 at 06:06 PM

Airmand gravatar image

Airmand
46 3 6 7

do you have skype? ill show you

Jul 09 '11 at 06:22 PM Babilinski

i can download it but i dont know how long it will take. Im currently in afgahnistan so my internet is a bit slower.

Jul 09 '11 at 06:34 PM Airmand
(comments are locked)
10|3000 characters needed characters left

ahh well are you using java or C#?

more ▼

answered Jul 09 '11 at 06:36 PM

Babilinski gravatar image

Babilinski
119 40 42 43

javascript

Jul 09 '11 at 06:37 PM Airmand
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x852
x314
x155
x78
x30

asked: Jul 09 '11 at 04:04 PM

Seen: 3750 times

Last Updated: Jul 09 '11 at 08:23 PM