- Home /
DontDestroyOnLoad Game Score/Lives
Hello everyone. I'm creating a space shooter game and I'd like the score and lives to carry over into the next scene of the game. I thought you would use the DontDestroyOnLoad method, but it seems to be having issues. I have my GUI script display for the Lives/Score counter attached to my Main Camera but carrying that over into a new scene freezes the game. Here is my GUI script attached to the Main Camera:
using UnityEngine;
using System.Collections;
public class MyGUI : MonoBehaviour {
public string nextlevel;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Player.score == 50) {
Application.LoadLevel(nextlevel);
}
}
void OnGUI () {
GUI.Box (new Rect (1700, 25, 300, 100), "Score : " + Player.score + " Lives: " + Player.playerlives);
}
}
Can anyone help with this?
Answer by Oniony · Feb 27, 2014 at 02:34 AM
Hmm I'm not sure if this would work but when Player.score == 50 you can set the score to a playerprefs variable and then in the next scene in the start method just reload the player.score from the playerpref.
Hmm...would playerprefs really be a good choice for just carrying over lives and such? I mean, I heard that it's more for saving positions in an RPG game or saving long-term data. $$anonymous$$y game is just a space shooter where I want the lives to continue carrying from the last scene.
Solved! I just created an empty gObj, stored a script in it with the playerlives and score variables as public and static, then used the DontDestroyOnLoad method in there. TY for the help, though!
Static didn't work anymore O_O
I posted a new question dealing with GetComponent.
Your answer
Follow this Question
Related Questions
Scoring with positions 1 Answer
GUI Text Score counter 3 Answers
Help! Why doesn't this work? GUI/Static vars Help! Javascript 1 Answer
Coin Counting Problem 0 Answers