Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by karammaks, Jul 17, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    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
     
  2. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    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.
     
  3. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i dont think the 2nd solution will work , would you tell me hoe to code the first solution ?
     
  4. Kawaburd

    Kawaburd

    Joined:
    Jul 22, 2013
    Posts:
    16
    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. http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
     
  5. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    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 ?
     
  6. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    you must use playerprefs
     
  7. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    did you read last reply ???:confused:
     
  8. Kawaburd

    Kawaburd

    Joined:
    Jul 22, 2013
    Posts:
    16
    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.
     
  9. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    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
     
  10. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Countless ways, perstiant object with DoNotDestroyOnLoad, playerPrefs, or your own method of serilzation like json or XML or even a database like sql
     
    alarm656 likes this.
  11. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    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.
     
  12. alarm656

    alarm656

    Joined:
    Jul 6, 2013
    Posts:
    111
    Watch this training video: