I am pretty sure I am doing something wrong but I don’t know what, since I never used it before and I can’t find anything on the internet.
Basically I want to keep the variables values from one scene to another.
So my first scene I’ve got and EmptyGame Object that displays the player hp and points and the target is the player(gameObject) form that scene.
First Image → http://i.imgur.com/aHtShSY.jpg
On the next scene I’ve got the exact same things but I also got a DontDestroy(EmptyGameObject) which constains the following code.
Second Image → http://i.imgur.com/DaYItQ1.jpg
Problem is that the hp and points are reseted when I change level.
#pragma strict
function Start()
{
DontDestroyOnLoad(transform.gameObject);
}


such things usually happen when you reload the scene your object is created in so the object is created again and you end up having more than one (as the others don’t get destroyed). you could circumvent this by having a scene just for loading such stuff which is never loaded again. or have a static bool in the script which is set to true in start and the script also checks wether it already has been initialized. if so it simply destroys itself (or the gameobject it lives on). not that destroying of gameobjects is not instant but they are flagged for destroy and removed at the end of frame so there could still be an overlap in this frame.
Yeah you need to put DontDestroyOnLoad on the object in the first scene, and then don’t have the object included at all in the second scene. This can make it hard to work with though since then you have to always load the first scene and run it before you can test the second scene, so you might be better off using a Singleton: http://clearcutgames.net/home/?p=437
That way it will be created if you run the second scene on its own, but it will be loaded if you go from the first scene to the second.
So this is a prefab and this code it’s inside an empty object.
What should I change ?
var target : GameObject;
private var plyHP : PlayerHealth;
private var plyPoints : PlayerPoints;
private var maxHealth : int = 100;
static var currentHealth : int;
static var currentPoints : int;
private var drawRestart = false;
function Start()
{
plyHP = target.GetComponent(PlayerHealth);
plyPoints = target.GetComponent(PlayerPoints);
DontDestroyOnLoad(transform.gameObject);
}
function Update()
{
currentHealth = plyHP.health;
currentPoints = plyPoints.startPoints;
if(currentHealth <= 0)
{
drawRestart = true;
currentHealth = 0;
}
}
function OnGUI()
{
GUI.Box(Rect(10,10,120,20),"Health: "+currentHealth + "/" + maxHealth);
GUI.Box(Rect(10,40,120,20),"Points: "+currentPoints);
if(drawRestart == true)
{
if(GUI.Button(Rect(Screen.width/2,Screen.height/2,100,50),"Restart"))
{
Application.LoadLevel("Level1");
}
}
}
Assuming the name of the script is “HealthTracker”, I think this should work:
static var instance : HealthTracker
var target : GameObject;
private var plyHP : PlayerHealth;
private var plyPoints : PlayerPoints;
private var maxHealth : int = 100;
static var currentHealth : int;
static var currentPoints : int;
private var drawRestart = false;
function Awake()
{
if(instance == null)
instance = this;
else
Destroy(gameObject);
}
function Start()
{
plyHP = target.GetComponent(PlayerHealth);
plyPoints = target.GetComponent(PlayerPoints);
DontDestroyOnLoad(transform.gameObject);
}
function Update()
{
currentHealth = plyHP.health;
currentPoints = plyPoints.startPoints;
if(currentHealth <= 0)
{
drawRestart = true;
currentHealth = 0;
}
}
function OnGUI()
{
GUI.Box(Rect(10,10,120,20),"Health: "+currentHealth + "/" + maxHealth);
GUI.Box(Rect(10,40,120,20),"Points: "+currentPoints);
if(drawRestart == true)
{
if(GUI.Button(Rect(Screen.width/2,Screen.height/2,100,50),"Restart"))
{
Application.LoadLevel("Level1");
}
}
}
This will keep whichever one is loaded first and destroy any extra ones when loading a new scene if there’s already a copy in there.
It didn’t work, and I changed HealthTracker with the script name.