- Home /
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);
}
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
Answer by aldonaletto · Jul 09, 2011 at 04:23 PM
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);
}
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 $$anonymous$$enu but now i get NullReferenceException Shop$$anonymous$$enu.BuyRange () (at Assets/Scripts/Shop$$anonymous$$enu.js:14) Shop$$anonymous$$enu.OnGUI () (at Assets/Scripts/Shop$$anonymous$$enu.js:9)
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!
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.
im sorry i didnt even see the comments you were leaving. I believe the scripts you edited of $$anonymous$$e work now. I just have to create a level 3 and see if the gunRange increased after it was bought
and thank you for the explanation. it is much clearer now
Answer by Babilinski · Jul 09, 2011 at 04:44 PM
if(points = 100) { uprange up = (Range).GetComponent("Range"); eh.uprange(+10); }
and in your ange class you should have
uprange + range = currange
Answer by Airmand · Jul 09, 2011 at 05:07 PM
sorry im very new to coding and unity. What would my range class be. and im not sure how to read psuedocode. :P
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;
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 A$$anonymous$$A curent range
im sorry i just really dont know how to read your pseudocode. What is cur.range supposed to be?
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;
Answer by Airmand · Jul 09, 2011 at 06:06 PM
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...
i can download it but i dont know how long it will take. Im currently in afgahnistan so my internet is a bit slower.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Storing transform information in a variable? 1 Answer
Call a method whenever the assigned variable gets altered 2 Answers
Collision object and access to a script variable? 1 Answer
How to access a variable in a script on the same object. 1 Answer
C# access another C # without using "GetComponent"? 1 Answer