- Home /
Keeping score problems dontdestroyonload
Ok I have been reading all articles here regarding keeping scores and still I can't get it to work properly. I just do not understand the right syntax on how to implement things. I am sure I am nearly there, but I get lost when trying to implement things.
I have a C# script that handles my game logic for a level:
First, I set my level variables:
public class GameLogic : MonoBehaviour {
public GUIText countText;
public GUIText targetCity;
public GUIText gameOverText;
public GUIText endScoreText;
public GUIText timerText;
public Texture2D bgImage;
private int count;
public GameObject cityPrefab;
List<MyCity> mycities;
public float finalScore; // I WANT THIS VALUE TO BE AVAILABLE IN MY NEXT LEVEL.
private float startTime;
//Rest of script executes fine; //Then, my level runs until the conditions for ending the game have been met (in this case = Time's up):
public void GameOver ()
{
gameOverText.text = "Time is Up!";
endScoreText.text = "You have found " + count.ToString() + " cities, great work!";
Destroy (GameObject.FindWithTag("EmptyCity"));
Destroy (GameObject.FindWithTag("City"));
Destroy (GameObject.FindWithTag("TargetCity"));
finalScore = count; //SO I KNOW HAVE A VALUE FOR THE FINAL SCORE OF THE LEVEL.
// THE GAME PAUSES FOR 7 SECONDS, THEN LOADS THE NEXT SCENE. HOW DO I GET THE VALUE OF finalScore in my next scene?
StartCoroutine(Wait());
}
IEnumerator Wait() {
yield return new WaitForSeconds(7);
Application.LoadLevel (3);
}
Level3, is just a simple scene where I congratulate the player with his performance. This is where I fail to get what I need to do to have the public float 'finalScore' from this level and load it into a GUIText object and display it on screen in my new scene.
I know I need to use Dontdestroy onload, but I fail to understand how that works when dealing with my public float finalScore.
Where I go wrong is linking the public float to a gameobject and making that game object available via dontdestroyonload to my next scene.
As I have tried so many things already, I would greatly appreciate if you could help me with the exact code lines I need to to this. Otherwise I am afraid that I make a mistake in the syntax.
Thanks Simon. I went that route and it works like a charm now. Still, I do want to learn about the Gameobject / dotndestroyonload approach at some point, but for now, this will do perfectly fine!
Answer by Simon-Larsen · Apr 04, 2014 at 12:43 PM
Okay, so as I can understand you want you save a highscore so you can access it even after you switch scenes. Now, this is of course possible using the DontDestroyOnLoad method, but instead I recommend you use PlayerPrefs. Think of PlayerPrefs as a way to save values outside of your scenes. For your highscore you could set its value in your first scene like so:
void SetScore() {
PlayerPrefs.SetFloat("Player Score", 10.0F);
}
You can then acces it in whichever scenes you want to access it from by using the GetFloat method, like so:
void GetScore() {
print(PlayerPrefs.GetFloat("Player Score"));
}
Answer by ABdoudj · Sep 05, 2018 at 09:12 AM
Thanks, i had the same problem with the normal score, when loading the same scene for a next round, i was struggling to make the Dontdestroyonload ,, but it didnt work , so i think i am gonna go with the playerprefs method with both normal score as well as the highscore But with different Keys,, and when the player score is below zero i will restart the scene BUt this time delete the stored key for the normal score ( in my case points) , that will be in a seperate function ! ;) Have fun Guys . . .
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad reset at certain point 2 Answers
DontDestroyOnLoad reset the gameobject 1 Answer
Don't understand how to use DontDestroyOnLoad. 2 Answers
score broken, score loads on new scene but then errors 0 Answers
GetComponent doesn't work 2 Answers