- Home /
how to get other script variable from other scene?
how to get other script variable from other scene?
Now, I have two java scripts from different scene(login.js, login scene and upload.js, upload scene )
I hope can get a variable from login scene in upload scene.
Used for android.
I'm Beginner, please give me a example code. My English is very pool, hope we can know.
thank you!
Answer by prototype7 · Apr 10, 2013 at 10:41 AM
Create singleton class, store your variable into it, access from other script in any scene.
login.js
class Login
{
// This is the singleton instance that is shared with everyone.
private static var Instance : Login;
// Everything that needs to get the singleton
// instance, calls this function to get it.
public static function GetInstance() : Login
{
if (Instance == null)
// This is the first time the function was called. Need to
// construct the shared instance.
Instance = new Login();
return Instance;
}
var state : boolean ;
// Private constructor. This is what forces the class
// to be a singleton since the only way to construct it
// is via GetInstance().
// NOTE: Private constructors are not normally allowed
// in JavaScript. But it is OK in Unity.
private function Login()
{
state=false;
}
function GetState() : boolean
{
return state;
}
}
upload.js
private var control : Login;
function Start()
{
control = Login.GetInstance();
Debug.Log(control.GetState());
// The next line would cause a compile time error due to
// the constructor's protection level.
// control = new Login();
}
Hi Fung if Test Ok, you should accept one of the answer to become green so others can easy to read. it's a checklist mark on the left
Answer by svenskefan · Apr 10, 2013 at 12:23 PM
Hi! The easiest way would be to create a static variable somewhere. You might even create a static class to hold your persistent data.
Yes, static variables are created when the game starts, and survive across scenes until the game finishes. But be aware that static variables are unique: if you have an enemyHealth static variable and multiple enemies in scene, for instance, when you kill one enemy all of them will fall dead because the health variable is the same for all instances.
You can declare a static variable this way:
static var playerHealth: int = 100;
The variable playerHealth is created with value 100 when the game starts, and keeps its current value even when new scenes are loaded (notice that the initialization to 100 is done only once, at program start)
Also, a static variable remains for the rest of the program. $$anonymous$$eaning if you need to pass a particular value from scene 1 to scene 2 but not anymore for the rest of the 20 other levels, your variable is there but will not be used ever again. Note that one integer will not get you running out of memory...
Answer by dreammakersgroupAdmin · Apr 10, 2013 at 10:53 AM
you have to save that variable to playersPref then take it again in other scene
Your answer
Follow this Question
Related Questions
Setting variable as type "Scene" 4 Answers
How to make two scripts share same variable? 1 Answer
Distance Variable Won't Change. What's Wrong With My Script? 1 Answer
Javascript 1 Answer