- Home /
Loading a scene and keeping original score
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
Answer by kurotatsu · Jul 13, 2012 at 03:39 AM
Updated the script as a copy and paste solution, so now it's actually functional.
Answer by whydoidoit · Jun 22, 2012 at 10:16 AM
You can make a separate object - an empty game object - and put your do not destroy script on that. Then make that object a singleton:
static var Instance : YourScriptName;
var score : int;
function Awake()
{
Instance = this;
DontDestroyOnLoad(this);
}
Now you can access your score like this:
YourScriptName.Instance.score += 100;
Thank you very much for your answer. However when I make a script for an empty game object, to turn that into a singleton it doesn't work. I named the singleton script '$$anonymous$$eepScore', In the first line of that script: 'static var Instance : YourScriptName;' I changed it to: static var Instance : ScoreSystem;. 'ScoreSystem' being the name for my scoring system script. When I try and attach the script to an empty game object it comes up with an error saying 'Cannot convert $$anonymous$$eepScore to ScoreSystem'. Also with the second code you provided 'YourScriptName.Instance.score += 100;' do I make that a separate script altogether and attach it to my empty game object?
In response to whydoidoit, Your making a static reference of the script in which you're writing this code. So you created a script called $$anonymous$$eepScore which is the one you're putting this code into. You're making a static reference of the $$anonymous$$eepScore script. You can change the values such as score in your scene with other scripts by doing $$anonymous$$eepScore.score, but you'd have to make score static.
Answer by kurotatsu · Jun 23, 2012 at 09:03 AM
I did it via learning about player preferences here: http://answers.unity3d.com/questions/250734/player-prefs-saving-position-script-assistance.html
There are other methods as well, but I used player prefs to also save player profiles, and carry them over to other scenes too. it's incredibly easy to save an integer using player prefs. Also refer to function Onlevelwasloaded() or in your function Awake to load your score in the next screen from your player pref save.
Hope this helps.