Hello I’m trying to load from one scene to another while keeping the original score. The game I’m working on involves collecting coins to add to a score, over multiple scenes. I wish to go from ‘Scene 1’ to ‘Scene 2’ while keeping the score accumulated in ‘Scene 1’ to be used and added to in ‘Scene 2’.
If it helps the script I’m using for the scoring system is:
var score = 0;
var scoreText = "Score: 0";
var mySkin : GUISkin;
function OnTriggerEnter(other : Collider ) {
if (other.tag == "Coin") {
Debug.Log("Other object is a coin");
score += 140;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
else if (other.tag == "Rock" && score > 0) {
Debug.Log("Other object is a rock");
score += -100;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
}
else if (other.tag == "Tree" && score > 0) {
Debug.Log("Other object is a tree");
score += -90;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
}
}
function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (250, 10, 200, 400), scoreText.ToString());
To transition from scene to scene I’m using this script, attached to a ‘Barrier’:
function OnTriggerEnter ()
{
Application.LoadLevel ("Scene 2");
}
I’ve tried using a DontDestroyOnLoad script which works to a certain extent. It keeps the score, and after collecting coins in ‘Scene 2’ they add to the original score.
However, upon activating the scene-transition script mentioned earlier the next scene loads with the ‘Player’ starting directly after it collided with the ‘Barrier’, which activates the scene-transition script. This is a bit of a problem as the ‘Player’ needs to start from the very beginning of ‘Scene 2’. Without the DontDestroyOnLoad script the ‘Player’ does start at the very beginning of the next scene but the score isn’t intact.
I apoligise for the long question. Thank you for any answers or feedback,
-Ben