How to Show Score in Unity: Simple UI Text Display
To show a score in Unity, create a UI Text element and update its
text property from a C# script that tracks the score value. Use UnityEngine.UI.Text to display the score on screen and update it whenever the score changes.Syntax
Use a UI Text component to display text on the screen. In your script, declare a Text variable to reference this UI element. Update the score by changing the text property of this Text object.
Example syntax:
public Text scoreText;- Reference to the UI Text.int score = 0;- Variable to hold the score.scoreText.text = "Score: " + score;- Update displayed text.
csharp
using UnityEngine; using UnityEngine.UI; public class ScoreDisplay : MonoBehaviour { public Text scoreText; // UI Text component private int score = 0; // Score value void Start() { UpdateScoreText(); } public void AddPoints(int points) { score += points; UpdateScoreText(); } void UpdateScoreText() { scoreText.text = "Score: " + score; } }
Example
This example shows a complete Unity script that updates a score displayed on screen. Attach this script to a GameObject and link a UI Text element in the Inspector. Calling AddPoints increases the score and updates the UI.
csharp
using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; private int score = 0; void Start() { scoreText.text = "Score: 0"; } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { AddPoints(10); // Add 10 points when space is pressed } } public void AddPoints(int points) { score += points; scoreText.text = "Score: " + score; } }
Output
When you press the space bar, the score displayed on screen increases by 10, e.g., "Score: 10", "Score: 20", etc.
Common Pitfalls
- Forgetting to link the UI Text component in the Inspector causes a
NullReferenceException. - Not updating the
textproperty after changing the score means the display won't change. - Using
TextMeshProrequires importing the package and usingTMPro.TextMeshProUGUIinstead ofUnityEngine.UI.Text.
csharp
/* Wrong: Not assigning UI Text in Inspector */ using UnityEngine.UI; public Text scoreText; void Start() { scoreText.text = "Score: 0"; // NullReferenceException if scoreText is null } /* Right: Assign UI Text in Inspector and check for null */ using UnityEngine.UI; public Text scoreText; void Start() { if(scoreText != null) { scoreText.text = "Score: 0"; } }
Quick Reference
- Create a UI Text element: GameObject > UI > Text.
- Drag the Text component to your script's public
Textfield in Inspector. - Update the score variable in your script.
- Set
scoreText.text = "Score: " + score;to show the score. - Use
Input.GetKeyDownor game events to change score.
Key Takeaways
Use a UI Text component and update its text property to show the score.
Always link the UI Text in the Inspector to avoid null errors.
Update the displayed text every time the score changes.
Use simple methods like AddPoints to manage score changes.
Test score updates with input or game events to confirm display.