Introduction
In this article, we are going to learn how to update the UI of a game in real-time. We will see how to modify the text, counters, and images using a script. Here, we will be using the unity engine to power our game. so, let's dive into the content.
Steps to Update the UI
Step 1: Clear the Text Field from the Text gameObject, so that it doesn't say anything.
Step 2: Create a new script called ScoreBehaviour and open it up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreBehaviour : MonoBehaviour
{
private Text thisText;
private int score;
void Start()
{
thisText = GetComponent<Text>();
// set score value to be zero
score = 0;
}
void Update()
{
// When P is hit
if(Input.GetKeyDown(KeyCode.P))
{
// add 500 points to score
score += 500;
}
// update text of Text element
thisText.text = "Score is " + score;
}
}







