Search Unity

I need to save the score when the scene resets

Discussion in 'Scripting' started by masterbuilder335, Sep 11, 2021.

  1. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    I made a score system for when a character falls off and hits the ground it resets the scene and gives +1 to the other characters score but since the scene resets the score goes away as soon as it pops up, I tried using Player.Prefs but it didn't work
     
  2. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public GameObject ScoreText1;
    9.     public GameObject ScoreText2;
    10.     public int Score1;
    11.     public int Score2;
    12.  
    13.     public string Tag1;
    14.     public string Tag2;
    15.  
    16.     void OnCollisionEnter(Collision collision)
    17.     {
    18.         if (collision.collider.tag == Tag1)
    19.             ScoreText2.GetComponent<Text>().text = "Player 2: " + Score2;
    20.  
    21.         if (collision.collider.tag == Tag2)
    22.             ScoreText1.GetComponent<Text>().text = "Player 1: " + Score1;
    23.     }
    24.  
    25.     public void Start()
    26.     {
    27.         this.SetInt();
    28.     }
    29.  
    30.     public void GetInt()
    31.     {
    32.         Score1 = PlayerPrefs.GetInt("Player1", 0);
    33.  
    34.         Score2 = PlayerPrefs.GetInt("Player2", 0);
    35.     }
    36.  
    37.     public void SetInt()
    38.     {
    39.         PlayerPrefs.SetInt("Player1", Score1);
    40.  
    41.         PlayerPrefs.SetInt("Player2", Score2);
    42.     }
    43. }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    PlayerPrefs will work if you use it properly, but it's really meant for storing data when the game ISN'T running. Your code above fails because you are ALWAYS calling SetInt() every Start, which happily wipes out any stored data from before.

    Either fix your logic, or instead, use a GameManager type construct to store the score. Google up any number of "how to make a game manager" videos, or just use a construct like this:

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    Or just make it
    static
    if you prefer1
     
    masterbuilder335 likes this.
  4. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    Okay thx, I will look it up!
     
  5. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    so do I make a GameManager in my scene and attach this code to it?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I see this question enough that I decided to answer it once and for all and you can tell me if I have done so successfully! Here is what I have created, two files:

    ULTRA-simple static solution to a GameManager:

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    Each file has notes at the top of it telling you how to use it. Can you please look it over and see if it makes sense? You will need to call GM.InitGame() at the start of every game, and call GM.AddScore() to add points, and then put the GMShowScore on whatever Text object you want to display your score.

    EDIT: see attached full scene using the above:
     

    Attached Files:

    Last edited: Sep 11, 2021
    masterbuilder335 likes this.
  7. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    the only thing that doesn't make sense to me is how I can't put it in the scene
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    AHA! Thanks for the feedback. Good point. You do NOT put GM into the scene at all. It is just a static class that lives as part of your game.

    However you DO put GMShowScore into your scene, in places such as:

    - during the game, put it up in the upper corner to show the score

    - in the GAME OVER screen, show it somewhere so the user knows what he achieved

    - anywhere else you like.
     
    masterbuilder335 likes this.
  9. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    So do I make the script but not add it to any object?
     
    Kurt-Dekker likes this.
  10. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    So it's there in my scripts folder but not on any object in my scene
     
    Kurt-Dekker likes this.
  11. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    How do I put GMShowScore? Do I name the object that or do I add a GMShowScore script?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I edited the first response above... check out the Unity package (install it): it has a demo scene. You can see the UnityEngine.UI.Text object under a canvas, and that is the object that has the GMShowScore script!
     
  13. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    Oh, Sorry I didn't see that, thx!
     
  14. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    in the game controller in the add points section would I make it detect the collision of the ground then add the points?
     
  15. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    and how would I make this support 2 ints?
     
  16. masterbuilder335

    masterbuilder335

    Joined:
    Jan 14, 2021
    Posts:
    69
    like when player 1 falls it add 1 to player 2's score and vice versa?