# 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** ](#gamedata-version2-with-properties-health-score)component.  Note: [**Required updates for GameData - V2**](../gameobjects-overview#project-1-gameobjects-scripts)

Steps Create GameObjects:

* **UI-Panel:  ScorePanel**
* **UI-Text:  ScoreText**
* **UI-Text:  HealthText**

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MFlAu9lBRrGNRAKQo3U%2F-MFlCe-GqLgsof1JoECy%2FScreen%20Shot%202020-08-27%20at%201.36.17%20PM.png?alt=media\&token=20a84461-5df0-4094-872c-da2746acbe7c)

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 ![](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/assets/Screen%20Shot%202019-01-29%20at%206.26.07%20AM.png)\
The **HealthText has it's Rect Transform set to Middle-Left,** as defined relative to it's Parent - the ScorePanel

![](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MFlAu9lBRrGNRAKQo3U%2F-MFlChiz2_VGKBrRforE%2FScreen%20Shot%202020-08-27%20at%201.36.24%20PM.png?alt=media\&token=3b632538-f91a-4706-b697-3be125334c5e)

**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. &#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

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

### 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. ![](https://github.com/kdoore/cs2335_v2/tree/e629ce99d3c4e27e0dc9a8b416a206a1d7051d22/assets/Screen%20Shot%202019-01-29%20at%205.57.40%20AM.png)

![Optionally Add Canvas Group ](https://3353821304-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgOacVpbicrqehO%2F-MHSJw5GGtAS4RgdqfDD%2F-MHSN3l68Zm-LSwlxOzo%2FScreen%20Shot%202020-09-17%20at%201.41.54%20PM.png?alt=media\&token=f40dded5-b00f-41e8-88cd-a8757c28b4dc)

## Class: PlayerStats.cs: &#x20;

#### **Requires update to GameData - see** [**GameData-V2 code**](#gamedata-version2-with-properties-health-score)&#x20;

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

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

```
