PlayerStats Version1

Player Stats Display will be our first use of UI-GameObjects.

We'll Use one UI-Panel and 2 UI-Text elements to display the Score and Health values that are stored in the GameData component.

Steps Create GameObjects:

  • UI-Panel: ScorePanel

  • UI-Text: ScoreText

  • UI-Text: HealthText

See the following Pages for more info on working with UI Elements: UI Elements: link

Details: 1. Add 1 UI-Panel Element to the Hierarchy, name it: ScorePanel. This initial creation of a UI element actually causes creation of 3 gameObjects that are added to the Hierarchy:

  • 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

  • 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 Left Side of the Panel.

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

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

Attach the PlayerStats script to the ScorePanel gameObject. Populate the Inspector fields: ScoreText and HealthText in the Inspector using the ScoreText and HealthText GameObjects.

Class: PlayerStats.cs:

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

public class PlayerStats : MonoBehaviour {

    //make connection with Text elements in the inspector
    public  Text healthText, scoreText;
    // Use this for initialization

    void Start()
    {
        UpdateDisplay();
    }

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

    public void UpdateDisplay(){
        healthText.text = "Health: " + GameData.instanceRef.Health;
        scoreText.text = "Score: " + GameData.instanceRef.Score;

    }
} // end Class

Code Version with no Public Variables

###Class: PlayerStats.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerStats : MonoBehaviour {

    //make connection in this script
    public  Text healthText, scoreText;
    // Use this for initialization

    void Start()
    {
    //here we make the connection with the variable and the gameObject's component using script -
    //Important, make sure the GameObject's name, (in quotes) matches the actual GameObject name in the Unity scene.
        healthText = GetComponent< Text >();
        scoreText = GetComponent< Text >();
        UpdateDisplay();
    }

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

    public void UpdateDisplay(){
        healthText.text = "Health: " + GameData.instanceRef.Health;
        scoreText.text = "Score: " + GameData.instanceRef.TotalScore;

    }
} // end Class

Last updated