Hi
i have 2 scenes in my game , Game Playing scene and Game Over scene , i have Score object in the first scene , and if the player lose , i want to put the score amount in the Game Over scene , how to do so ? im new in unity and scripting
Iām sure there are better ways but you could either
- Save the score in a text/ini file and read it in the game over scene
- Use GetComponent and access the variable storing the score but I donāt know if it works between scenes.
i dont think the 2nd solution will work , would you tell me hoe to code the first solution ?
If you just want it to persist during that particular play, store the score in an object that persists between stages.
Specifically, add this into the Start() function of your score script. Preferably in a āgodā object that doesnāt display anything on itās own and just gets referenced by other stuff.
Object.DontDestroyOnLoad(gameObject);
If you want to store the high scores between plays (really just basic saving/loading of a variable), get acquainted with PlayerPrefs. Unity - Scripting API: PlayerPrefs
i have used the PlayerPrefs and watched this video which was very useful , but the problem happened to me when the score is being saved and doesnāt change even if i restart the game, unless i get higher score ( its very nice to make it as the highest score ) , but i wanted to save the score just for the Game Over scene , and if i restart the game and get different score , it will be shown , not show the highest score , how to do so ?
you must use playerprefs
did you read last reply ???
If you only want it displayed for a single playthrough, donāt bother with playerprefs. Just keep the score object persistent. And if you put in some sort of ārestartā or āplay againā option on the game over screen, have that also reset the score to 0.
I just tried to use ObjectDontDestroyOnLoad(gameObject);
and I got following error message:
MissingReferenceException: The object of type āGameObjectā has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
levelCreator.FixedUpdate () (at Assets/Scripts/levelCreator.cs:92)
error message in another script not in that script I put "ObjectDontDestroyOnLoad(gameObject);
and when I play again every time Iām having several scores in the scene and Main Camera
Countless ways, perstiant object with DoNotDestroyOnLoad, playerPrefs, or your own method of serilzation like json or XML or even a database like sql
Thank you for your reply cmcpasserby. Iām supper New and I donāt understand your reply. But I solved my problem. I have add another camera called it āGameOverCameraā instead of changing āsceneā when the player dyes I set GameOverCamera active (true). everything works great. Now Iām changing coordinates and style of scores))
thank you again.