- Home /
Adding varibales from other scripts
I have three scripts:
Variable:
static var score = 0;
Present 1:
var score= Variables.score;
function OnTriggerEnter ()
{
score = score+1;
}
and win:
var score= Variables.score;
function Update(){
if(score==1){
Application.LoadLevel("Level 1");
}
}
I want it to go to the next level once var score equals 1 (for now). It isnt working and im not sure why because it not giving me any errors. Please help.
Answer by The Kracken · Dec 10, 2013 at 02:02 AM
A few questions. these are all in different scripts? Why not just put
function OnTriggerEnter ()
{
Application.LoadLevel("Level 1");
}
unless you plan on adding more "score" points to later levels. is score used anywhere else? i see its static, maybe another object is setting it back to 0 before it get to the update loop.
I will add more score points in later but I was gonna use one for now. I can't find any object or anything that would set it back because I dont have any that are supposed to do that.
Answer by archie_azares · Dec 10, 2013 at 03:02 PM
you could also remove the update function and put the if statement inside the function onTriggerEnter after increasing the value of score variable and yes, your code might work but it is just not simplified.
Answer by StormSabotage · Dec 10, 2013 at 02:28 PM
How i understood it's a different scripts ?
First is :
var score= Variables.score;
function OnTriggerEnter (){
score = score+1;
}
and second is :
var score= Variables.score;
function Update(){
if(score==1){
Application.LoadLevel("Level 1");
}
}
if so, that will not work, because in first script you load your 'Variables.score' into local variable 'score' and raise it, but in second script you again load your 'Variables.score' which wasn't changed and still equals 0.
To fix it you must change your first script like that:
function OnTriggerEnter (){
Variables.score = Variables.score+1;
}
or
function OnTriggerEnter (){
Variables.score++;
}
Your answer
Follow this Question
Related Questions
Collider is only working on the first frame 0 Answers
Collision with renderer.enabled? 0 Answers
Sound on collision 3 Answers