scene to scene variables

I want to save variables from one scene, and bring them into another one. An example would be saving your health from a fight, to the world, to another fight (in a Final Fantasy or Pokemon style battle system). It would also help for keeping score. Is there anyway to do this?

Use DontDestroyOnLoad in the script for the object that contains the variables you want to retain from one scene to another. Also see here about creating singletons.

PlayerPrefs Class is also very helpful if you want to save any of those variables for use in multiple game sessions.

Full details are here: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

but the easiest way is to use something like this:

static var scorevar: int; 

function saveScores()
{
PlayerPrefs.SetInt("HiScore",scorevar);
} 

function getScores()
{
score1 = PlayerPrefs.GetInt("HiScore");
}

The word "HiScore" used in the example is just a key for setting and retreiving the value (int) that is saved. In other words, you can make it anything you want. You can save other data like strings if you use the correct sub class, all these options are listed in the player prefs script referance.

You can create an object in first scene, attach a sript to it and sen value of variable in this script. For example if you want to send user login script attachet to game object that will survife after second scene load should be:

// User login:

public var userLogin : String;

function Awake() {

// Do not destroy this game object:

DontDestroyOnLoad(this);

}

Set userLogin in first scene and read in second.

Complete example can be found in my blog http://unity-tutorials.blogspot.com/. Listing 20 and 21.

//first script
using UnityEngine;
using System.Collections;

public class First : MonoBehaviour {


public int test=false;


}



//second script

using UnityEngine;
using System.Collections;

public class second : MonoBehaviour {

void Update () {
if(Input.GetKey(KeyCode.A))
{
First.test=true;
//if you press a it change the variable from the //skript"first"
}
}

	
	}