Having problems passing a value from one script to another.
Here is my script.
I want to pass the currentvalue from my second script to my first script but it’s not working, I know i’m doing a simple mistake but its starting to frustration me now.
//first script
var currentlevel : int = 0;
function Update (){
var currentlevel = SwipeControl.currentValue;
}
//second script values i want to get. the name of this script is SwipeControl
public static var currentValue : int = 0; // current value
You need to look at the scope principle. To put it simple, when you create a variable after a “{”, it will die when comes the matching “}”.
When you create a global variable (not static, global this is important too), it lives the scope of the script. Your var currentLevel as it is now, is created as global and then a new one is created again and dies each frame and the compiler recreates a new one right after but even though it has the same name it is a total different one at a different memory location (I think you would get an error with this using VS and C or C++).
Now, it is different for the functions, they are implemented outside the update but use inside so they can access any variables alive at the time of calling.
Now try this way:
var currentlevel;
function Update (){
currentlevel = SwipeControl.currentValue; // here I took var on the front
Debug.Log(currentlevel);
}
function OnMouseDown () {
Debug.Log("mouse enter");
if (currentlevel == 0){
Debug.Log("Loading Level");
Application.LoadLevel(1);
}
else {
if (currentlevel == 1){
Application.LoadLevel(2);
}
}
}