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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Where to store Game Scores, Lives, Inventory etc between scenes...

Discussion in 'Scripting' started by paulxthompson, Sep 21, 2015.

  1. paulxthompson

    paulxthompson

    Joined:
    Aug 17, 2015
    Posts:
    6
    Hi All,
    I've been keeping my Lives and Scores in a script attached to the player object along with its sprites, colliders and controllers, because the player sprite is consistent between scenes this seems to work ok.

    But as my game develops from the basics into the niceties of proper screens for game over and level transitions, and alternative player characters, I think i'm going to need a better management of all of this than have it bolted directly to the player. Soooo....

    Where do you all think is the best place to keep the persistent script to track these things? In with the UI itself? An empty game object? My guess is that the player would continue to look after his collision, but knows nothing about scoring so would just pass the collided object name (or other value) to a scoring script that knows everything about scoring and nothing else.

    Anyway, this is the script i've got so far, and in case it's useful to anyone as new as myself, the key to persistence between Scenes is the static keyword and the DontDestroyOnLoad bit.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class Score : MonoBehaviour {
    8.  
    9.     public Text scoreText;
    10.     public int objectValue=15;
    11.     public Text livesText;
    12.     public int maxLives;
    13.     public static int score;
    14.     public List<string> collectedObjects;
    15.     public static int lives;
    16.  
    17.     void Awake() {
    18.         DontDestroyOnLoad(this);
    19.     }
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         //score = 0;
    24.         Debug.Log ("Score:"+score);
    25.         UpdateScore ();
    26.         //lives = 30;
    27.         maxLives = 3;
    28.         Debug.Log ("Lives:"+lives);
    29.         UpdateLives ();
    30.     }
    31.  
    32.     void OnCollisionEnter2D(Collision2D collision)
    33.     {
    34.         if (collision.gameObject.tag == "Collectible")
    35.         {   Debug.Log ("Collecting");
    36.             collectedObjects.Add (collision.gameObject.name);
    37.             Destroy (collision.gameObject);
    38.             score += objectValue;
    39.             UpdateScore();
    40.         }
    41.         if (collision.gameObject.tag == "Enemy")
    42.         {   Debug.Log ("Oops");
    43.             Destroy (gameObject);
    44.             lives+=1;
    45.             UpdateLives ();
    46.                       // TODO:
    47.                       // if (lives >= maxLives) Application.LoadLevel('GameOver');
    48.                       // else...
    49.             Application.LoadLevel(Application.loadedLevelName);
    50.         }
    51.     }
    52.  
    53.     void UpdateScore(){
    54.         scoreText.text = "Score:\n" + score;
    55.     }
    56.  
    57.     void UpdateLives(){
    58.         livesText.text = "Lives:\n" + (maxLives - lives);
    59.     }
    60. }
    61.  
     
  2. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    There's a relatively well explained example in the following video :

    You should find the first interesting part at 7min approximately when he explains and create the GameInformation.cs CSharp file. The 4 videos following it explain how to save and load the data (so 10 to 14).

    From that alone, you should be able to work it out.
     
  3. paulxthompson

    paulxthompson

    Joined:
    Aug 17, 2015
    Posts:
    6
    Thank you @Max_Bol that's exactly what I was after, and more: I didn't know about the automatic getter and setter syntax which is going to come in very handy.

    Mind you, figuring out how to make static elements play nice with none static ones is a right pain, but i'm persevering.
     
    Last edited: Sep 22, 2015