ModifyPlayerData

The following script can be added to PlayerStats ( or any gameObject in a scene) To provide a way to configure a UI Button in the Inspector Panel to modify GameData Score or Health. It's necessary to use a helper script like this since GameData is a singleton and therefore is not available as a gameObject in any scene other than BeginScene, to configure calling public methods to change GameData values.

PlayerStats Prefab: UpdateText: Note: I have added a new UI-Text field to PlayerStats Prefab to display text to indicate when data's been updated.

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

//Add to ChoicePanel or an empty gameObject 
//to allow easy modificationof Score, Health 
//configure to be executed for a Button onClick event in inspector
public class ModifyPlayerData : MonoBehaviour
{
    [SerializeField]
    Text updateText;
    string displayText;
    string newLine = System.Environment.NewLine;  //add newline

    private void Start()
    {
        // GameData.instanceRef.onPlayerDataUpdate.AddListener(UpdateDisplay);
        displayText = "";
        UpdateDisplay();
    }

    public void IncreaseScore(int value)
    {
        GameData.instanceRef.Add(value);
        displayText = "Increase" + newLine + "Score: " + value  ;
        UpdateDisplay();
    }

    public void DecreaseScore(int value)
    {
        GameData.instanceRef.Buy(value);
        displayText = "Decrease" + newLine + "Score: " + value  ;
        UpdateDisplay();
    }

    public void IncreaseHealth(int value)
    {
        GameData.instanceRef.BoostHealth(value);
        displayText = "Boost Health: " + value ;
        UpdateDisplay();
    }

    public void DecreaseHealth(int value)
    {
        GameData.instanceRef.TakeDamage(value);
        displayText = "Decrease" + newLine + "Health: "  + value ;
        UpdateDisplay();
    }

    public void UpdateDisplay()
    {
        if (updateText != null)
        {
            updateText.text = displayText;
        }

    }   

}

Last updated