GameData Version2

This version of GameData includes C# Properties to provide read-only access to private variables: score, health.

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

//version 2 F20
public class GameData : MonoBehaviour
{
    public static GameData instanceRef;  //null    singleton global variable

private int score;
private int health;

public int Score   //PROPERTY
{
    get { return score;  } //read Only Access
}

public int Health . //PROPERTY  
{
    get { return health; }  ///readOnly access

}

//Initialize in Awake so it can be accessed in Start for dependent GameObjects
void Awake()
{
    if (instanceRef == null)
    {
        instanceRef = this;   //the object currently executing the code
        DontDestroyOnLoad(this.gameObject);
    }
    else
    {
        DestroyImmediate(this.gameObject);
        Debug.Log(" Destroyed GameData Imposter ");
    }

    score = 0;
    health = 100;

} //end of awake

//Increases Score, called in PlayerController
public void Add(int value)
{
    score += value;
    Debug.Log("Score updated " + score);
}

public void TakeDamage(int value)
{
    health -= value; //subtract points from health
    Debug.Log("Health updated " + health);
    if (health <= 0)
    {
        Debug.Log("Health is ZERO gameOver ");
    }
}

public void ResetGameData()
{
    score = 0;
    health = 100;
}

}//end GameData

Last updated