> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/project1-code/playerstatsdisplay-version1.md).

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

Steps Create GameObjects:

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

![](/files/-MFlCe-GqLgsof1JoECy)

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

![](/files/-MFlChiz2_VGKBrRforE)

**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 ](/files/-MHSN3l68Zm-LSwlxOzo)

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

#### **Requires update to GameData - see** [**GameData-V2 code**](/cs2335/project1-code/playerstatsdisplay-version1.md#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

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/project1-code/playerstatsdisplay-version1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
