hot to transfer score amount from 1st scene to 2nd one ?

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

  1. Save the score in a text/ini file and read it in the game over scene
  2. 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 ???:face_with_spiral_eyes:

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

1 Like

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.

Watch this training video: