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

GameData Version2 - With Properties: Health, Score

Last updated

Was this helpful?