Looking for some help regarding GUI in Unity ?

I am new to unity game development and I’ve been trying to make a game where a sphere goes upwards avoiding many obstacles and scoring points as per the sphere moving upwards.
I multiplied Time.time100 and using print(Time.time100) i am able to see my score in the console but I don’t know how to display it out on a GUI box inside unity. I did a little bit study but no luck. Any help is appreciated.

Another thing is that when i press “w” then my sphere goes upwards and scores increase accordingly so I wrote something like this :

`if(Input.GetKey(“w”)

{
print(Time.time*100);
}
`
This is working when i press w the score increases but I also want to use a condition that when I press w and when it moves upwards then increase my score.

Any help is appreciated.

To show some text in the new GUI (one of many ways):

Define a canvas, define a text field in the canvas. Then in your script, add a using-directive:

using UnityEngine.UI;

Then, add a variable:

    public Text myText;

In the Unity-Inspector, select your Object with the script. Pull the Text-Field to the field in the script named “myText”. Then you can access the textfield content:

void Update()
{
    // ...
    myText.text = "Whatever text you like.";
    // ...
}

Hope that helps.

You need to use new UI system in unity. (Edit: You can also use the old UI system, sorry I have done this using the new UI system, you can use whatever you prefer)

  1. Right click in your hierarchy window, In the pop up choose UI and click Text.

  2. In your score multiplying script, you need to pass the score values to the text.

     using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;
    
     public Text scoreText; // drag the text and drop it in here in the hierarchy
     public int scoreCollected; // this will be your score
    
     void Update()
     {
        scoreText.text = scoreCollected.ToString(); // This will add your score to the UI          
      }
    
  3. To create a condition and add the score, you need to create a function something like this

    public void scoreCollector(int scoreADD)
    {
    if()// Your Input key down line goes here
    {
    scoreCollected+=scoreADD; // This will add your score for each key press
    }

    }

You don’t HAVE to use the new UI, the old GUI is still a viable option:

public int score = 0;//can be a float as well

void OnGUI() {
     GUI.Box(new Rect(0,0,200,100), "Score: " + score);
}