unity 3d student

Beginner B14 – GUI Text and Counters

How to display information onscreen with GUI Text components and display a counting variable.

Code Used (Javascript)

var Counter : int = 0;
  1.  
  2. function Update () {
  3.  Counter++;
  4.  guiText.text = "Counter is: "+Counter;
  5. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

23 Comments

    I am picking on details there, but convention usually advise to use lowercase for the first letter of a variable 🙂

    • No I totally agree, ordinarily I would , not sure what happened there!

    Seems strange that Unity can’t implicitly convert the type without having to hack around it with a “” + myValue.

    Should go into Unity 3 for when it releases. 🙁

    • Its not really that lance, its more efficient for data to be specifically typed and Unity can’t just assume that you’ll throw any kind of data in there hence this approach.

    • The “proper” way to convert the integer to a string would be to say Counter.toString() rather than append an empty string onto the beginning 🙂

    Thanks for all the vids. I am pretty new to Unity, just got it a day ago. So I was wondering if I wanted to have my character talk to other characters in the game world how would I go about doing that interaction?

    Would I first see if the player camera has collided with the character than check if enter has been pressed and then bring up a Gui text? Or is there a simpler way around it. Also what if I wanted a transparent black background behind the text to make it appear more professional…how would I do that?

    • you’d likely want to check if they were near the character either by distance or by putting a trigger area around the character. then allow the player to choose whether to interact or not. for more complex or professional GUI stuff use the GUI class – not something ive covered as yet on the site but look at the docs for it.

      http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

    I don’t know how to connect the update of score. I think that every time there is a collision between the bullet and the targets the Counter++ should occur but I don’t know how to connect those since the GUIText is not connect with the bullet nor the target objects….

  • Ignore my previous post…sorry….again! 🙂

  • Hey, great videos! I can’t get the GUIText stuff to work, if I add it as a componet I can never see it!
    Any ideas?

    • GUUtext components rely on the transform of the object they are attached to having an X and Y position of between 0 and 1 – this is because they operate in screen coordinates. So if you add the component to an object at for example (23, 76.123, 24) then the position of the text would be offscreen. Because of this it’s usually best to make a new GUItext object from the gameobject menu instead.

      • Then do I need to do a .find kind of code to find the GUI Text after the ball hits the target?

    How would you make a newline character in the text field of the GUIText Component?

    • As with a few languages – you place ‘\n’ (ignoring the apostrophes) to give a new line.

    Hi ! I have a beginner question !

    I did everything as this tutorial about GUI Text covered. Basically, they worked. However, when I first created my GUIText object and tried to move it around, I realized that the center of the object is not anywhere near the text like in any other objects. And when I tried to move it just a little bit, the center is still in my vision, but the text fled elsewhere and never came back. Please help me !!! Thank you very much !

  • Hey thanks for all the tutorials you’ve made, they’ve been a huge help to me so far 🙂 I’m a little stuck now though.

    var myTimer : float = 60.0;

    function Update ()
    {
    {
    GUIText.text = “Time Remaining”+myTimer;
    }
    if(myTimer > 0)
    {
    myTimer -= Time.deltaTime;
    }
    if(myTimer <= 0)
    {
    Debug.Log("GAME OVER");
    }
    }

    Can you tell me what i'm doing wrong :)?

    • Yes, you have a spare { when you first open the Update() function.

    Hey there,

    I was wondering, can you “click” on gui text, and then that Unity realizes that and then you go to another menu, or something like that?
    Or do you need a trigger area, (box, tick “trigger”) for it?

  • Hey, how would you go about applying this script to the 3D Text object in Unity?

  • Hey thanks for tutorial. How can i use for floats?

  • Hey Will,

    Thank you so much for the tutorials. I’m using Unity 4.6, in which GUI Text seems to have changed drastically. I’m getting the error as follows:

    MissingComponentException: There is no ‘GUIText’ attached to the “Canvas” game object, but a script is trying to access it.
    You probably need to add a GUIText to the game object “Canvas”. Or your script needs to check if the component is attached before using it.
    GUITextExample.Update () (at Assets/Scripts/GUITextExample.cs:9)

    If it’s a fundamental change that’s ok, but if there’s a quick workaround for the way 4.6 does it, I’d love to know!

    Thanks.

  • hey, how would you change the gui text to the value of a variable

Leave a Comment