Table of contents
1.
Introduction
2.
Steps to Update the UI
3.
What's in the code?
4.
Frequently Asked Questions
4.1.
What is unity3D?
4.2.
What is the script in unity composed of?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Realtime UI Update

Author Ankit kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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;
    }

}

What's in the code?

we're initialising a text class variable with the name thisText. This function is available after we add Using UnityEngine.UI at the top. In the start() function, The first statement assigns the thisText variable to the Text component attached to the gameObject, and the other simply sets the score to 0 initially. In the Update() function, we are defining an input control to add 500 points to the score each time we press the P key.

Step 3: save the script and head back to unity. After pressing P a few times, You will see the updated score.

Result 1

source

Result 2

source

Frequently Asked Questions

What is unity3D?

Unity3D is a powerful cross-platform 3D engine and a user-friendly development environment. Unity should interest anybody who wants to easily create 3D games and applications for mobile, desktop, the web, and consoles.

 

What is the script in unity composed of?

The script in unity is composed of the classes and game objects. Here, the functions are defined to perform various actions.

Conclusion

In this article, we have discussed how to update the UI of the game in real-time.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you enhance your knowledge regarding puzzles, and if you liked this blog, check other links.

Do upvote our blog to help other ninjas grow.

Happy Coding!"

Thank You
Live masterclass