- Home /
issues passing a value form one script to another
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
I don't see anything wrong with that code. What is it doing ?
Answer by fafase · Mar 23, 2012 at 09:07 PM
Would it be that you are creating twice the same variable. Once as global and once in the update function?
Answer by fafase · Mar 24, 2012 at 06:41 PM
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);
        }
     }
 }
I also changed the currentLevel == ("0") to currentLevel== 0 . I realized you actually don't need you could simply check if(SwipeControl.currentValue == 0){}
Your answer
 
 
             Follow this Question
Related Questions
When passing a value it becomes null 0 Answers
Instantiate question 1 Answer
Variable doesn't change when box hit trigger area 3 Answers
How to get a value from an array within another script. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                