Change Scene and Transfar a variable to it...

Hey everybody,

I just got a question:
How can I transfer a variable from a Script to another Script in another scene ?

The Idea:
When a trigger has been entered a scripts gets activated.
The script defines, that a new scene called “fightingScene” should be loaded.
also the script defines a “enemyLevel” (for example 7)
Now I want to load the fightingScene with transfering the “enemyLevel” through to handle the enemy level in the new scene…

How can I handle that?

Thank you so far…

something like this:

...
PlayerPrefs.SetInt("EnemyLevel", 7);
Application.LoadLevel(leveltoLoad);

then in the new scene add the following somewhere:

int enemyLevel = PlayerPrefs.GetInt("EnemyLevel");

you don’t even need to use playerprefs, you can create a scene independant class and store your variable in it. Then grab the variable from within the new scene

you can use statics var …
create an empty…
then create a script javas:

static var enemyLevel : int;

attach the script to the empty
create a prefab inside a folder on your project panel
drag the empty to the recent created prefab

then, in the trigger where you load the next scene:

var globalPrefab : GameObject;


function OnTriggerEnter ( other : Collider){
globalPrefab.GetComponent("scriptWithStaticVar").enemyLevel = 7;
Application.LoadLevel("battleScene");

}

One time you loaded the battleScene, you read the static var enemyLevel…

var globalPrefab : GameObject;
var globalScript;

function Start(){
globalScript = globalPrefab.GetComponent("scriptWithStaticVar");
if (globalScript.enemyLevel == 1){
  //Set enemy skill to 1
  }

else if (globalScript.enemyLevel == 2){
  //Set enemy skill to 2
  }
}

static vars are really useful to transmit information to others scenes :wink:

Thank you all for the well-discribed answers :slight_smile:
I’ll try the Siliziums Tomorrow :slight_smile:
Thank you so far

Ah… can’t believe I didn’t think of a static variable…
I was so caught up in thinking of Unity-specific stuff, I had forgotten a basic programming principal… /embarrassed…

hehe yeah, there are many ways to do anything in unity don´t worry :wink: