# 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

![](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/assets/Screen%20Shot%202019-01-29%20at%206.23.51%20AM.png)

See the following Pages for more info on working with UI Elements: [UI Elements: link](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/project-1-score-and-ui-elements.md)

**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. &#x20;
* 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**&#x20;
  * Use Rect-Transform Component panel to set the  anchors of the ScoreText so it is aligned to the Left Side of the Panel. &#x20;
  * Add UI-Text element as a child: name it: **HealthText**&#x20;
  * Use Rect-Transform Component panel to set the  anchors of the ScoreText so it is aligned to the Right Side of the Panel. &#x20;

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

The image below shows the ScorePanel has 2 UI-Text gameObjects as children in the Hierarchy Panel. In the Inspector Panel, the ScoreText has it's Rect Transform set to Upper-Left. The ScoreText's Transform is defined relative to it's Parent - the ScorePanel ![](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/assets/Screen%20Shot%202019-01-29%20at%206.26.07%20AM.png)

The image below shows that the ScorePanel is anchored using stretch-top setting in the Rect Transform component. Also, the HealthText and ScoreText gameObjects have been used to populate the corresponding fields in the PlayerStats script component. ![](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/assets/Screen%20Shot%202019-01-29%20at%205.57.40%20AM.png)

## Class: PlayerStats.cs:

```java
//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:
```

```java
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
```
