GameData Version1

GameData - Version 1

In the game we'll need an object that is persisted throughout the gameplay session so that we can keep track of score, lives, health, etc.

Below is a simple class definition for GameData, we'll add more to this as we progress.

This script will be attached to the GameManager in the first game Scene and will be persisted throughout the game since it uses the singleton design pattern.

Add GameData script to an Empty GameObject: named: GameManager in your starting scene, so it will be executed as a singleton and shows up as 'DontDestroyOnLoad' in the Hierarchy when the game is played.

How to use GameData Singleton To use the singleton reference in another class use the following syntax, for example, when calling the Add method from the PlayerController script:

Example of using GameData singleton in PlayerController.cs

GameData.instanceRef.Add( item.value );

GameData Class Definition

modified 4/10/2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Singleton Object to store all GameData
/// Is not destroyed when changing scenes
/// </summary>
public class GameData : MonoBehaviour
{
    public static GameData instanceRef; //null //variable that can point to a GameData object

    private int score;
    private int health;
    private int levelScore; //project 3

    /// <summary>
    /// Properties - provide read/write access to variables
    /// </summary>

        public int  Score
    {
        get { return score;     } //Read access
       // set { totalScore = value; }   //write access
    }

    public int Health
    {
        get { return health; } //read-only acccess
    }

    // Awake is called before Start() is called on any GameObject
    // Other objects have dependencies on this object so it must be created first
    void Awake()
    {
        if( instanceRef == null)  //this code hasn't been executed before
        {
            instanceRef = this; //point to object instance currently executing this code
            DontDestroyOnLoad(this.gameObject); //don't destroy the gameObject this is attached to
        }
        else  //this object is not the first, it's an imposter that must be destroyed
        {
            DestroyImmediate(this.gameObject);
            Debug.Log("Destroy GameData Imposter");
        }

        //initialize after destroying imposters
        score = 0;
        health = 100;
        levelScore = 0;

    } //end Awake

    //will be executed in PlayerController when colliding with a collectible
    public void Add( int value)
    {
        totalScore += value;
        Debug.Log("Score updated: " + score); //display score in console
    }

    public void TakeDamage( int value)
    {
        health -= value;
        Debug.Log("health updated: " + health); //display health in console
        if( health <= 0)
        {
            Debug.Log("Health less than 0"); //display health in console

        }
    }

    //called when restarting the miniGame
    public void ResetGameData()
    {
        score = 0;
        health = 100;
    }


}//end class GameData

Resets Score and Health to correct initial values. Public, so it can be executed from MiniGameManager Script.

Last updated