PlayerStats Version1

This project1 will use a UI-Panel with 2 children: UI-Text elements to display the score and health values that are stored in the GameData component. Note: Required updates for GameData - V2

Steps Create GameObjects:

  • UI-Panel: ScorePanel

  • UI-Text: ScoreText

  • UI-Text: HealthText

The images show the ScorePanel has 2 UI-Text gameObjects as children in the Hierarchy Panel. The ScorePanel is anchored using Stretch-Top setting in the Rect Transform component. The ScoreText has it's Rect Transform set to Middle-Right, as defined relative to it's Parent - the ScorePanel The HealthText has it's Rect Transform set to Middle-Left, as defined relative to it's Parent - the ScorePanel

Directions: 1. Add A UI-Panel Element to the Hierarchy, 2. Name it: ScorePanel. 3. If these are the first UI elements added to the Scene, follow the directions to configure the Canvas RenderMode

  • Canvas - the parent GameObject for all UI elements

  • Panel - used here as a container for 2 UI-Text elements

  • EventSystem - manages UI event-handling - if UI events don't seem to be working, make sure that one of these components is in the Hierarchy.

  • When any additional UI elements are added to the Hierarchy, the Canvas will be the parent element.

  • Modify the Canvas's RenderMode, set it to 'Screen-space Camera', and select the Main-camera as the render camera

Configure Rect-Transforms

  • Resize the Panel and Use the Rect-Transform component to set the Panel's anchors to the top of the canvas.

    • Add UI-Text element as a child: name it: ScoreText

    • Use Rect-Transform Component panel to set the anchors of the ScoreText so it is aligned to the Middle-Right Side of the ScorePanel.

    • Add UI-Text element as a child: name it: HealthText

    • Use Rect-Transform Component panel to set the anchors of the HealthText so it is aligned to the Middle-Left Side of the ScorePanel.

Attach Script to Panel ScorePanel

  • Attach the PlayerStats.cs script to the ScorePanel gameObject.

  • Populate the Inspector fields: ScoreText and HealthText in the Inspector using the ScoreText and HealthText GameObjects.

  • Optionally add a CanvasGroup component if you want to hide the PlayerStats Panel prior to MiniGameState.active state. Logic needs to be added for this functionality

The image below shows that Also, the HealthText and ScoreText gameObjects have been used to populate the corresponding fields in the PlayerStats script component.

Optionally Add Canvas Group

Class: PlayerStats.cs:

Requires update to GameData - see GameData-V2 code

//Feb 22, 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//Script component to update Score and Health Display

public class PlayerStats : MonoBehaviour {

    //make connection with Text elements in the inspector
    [SerializeField]
    Text healthText;
    
    [SerializeField]
    Text scoreText;
    // Use this for initialization

    void Start()
    {
        UpdateDisplay();
    }

    void Update(){ //called every frame - polling to see if data changed
        UpdateDisplay();
    }

    //This will eventually be executed as an event-listener so it is declared
    //as a public method
    public void UpdateDisplay(){
        healthText.text = "Health: " + GameData.instanceRef.Health;
        scoreText.text = "Score: " + GameData.instanceRef.Score;
    }
} // end Class

GameData Version2 - With Properties: Health, Score

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

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
    {
        get { return health; }  ///readOnly access
        
    }

    //TODO Add Properties for Score, Health

    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 of GameData

Last updated

Was this helpful?