• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Keavon · Apr 06, 2011 at 12:49 AM · guitextstaticguitextmodify

Script to change GUI text's displayed text value?

Hi. I want to use GUI Text to display a score, but it needs to change. Can I please have a basic code snippet to change the displayed text from a variable?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by e.bonneville · Apr 06, 2011 at 01:02 AM

Check out the docs for GUI Text. You'll specifically be interested in the text variable of a GUI Text.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Keavon · Apr 06, 2011 at 01:12 AM 0
Share

It works! Thank you! I checked the documentation but was unable to find it. A real quick question, is how would I call a specific gameobject (the GUI text) from that script attached to a different gameobject?

avatar image e.bonneville · Apr 06, 2011 at 01:37 AM 0
Share

Yes, but you'll have to create a reference to that specific GUIText component, e.g. something like public GUIText myGUIText in C# (`var myGUIText : GUIText` in JS) and then change the .text variable on myGUIText, like so: myGUIText.text = scoreVariable; You'll also need to assign a reference to that script in the Inspector.

avatar image
1

Answer by A_Devloper · Sep 16, 2014 at 07:10 AM

To find the GUIText object, you can also use GameObject.Find, like so:

 guiText = GameObject.Find("GUI_TEXT_NAME").guiText;

For effieciency, do this in the Start() function and save it to a private GUIText variable.

As of Unity 2.0, you can also use the GUI.Label function.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Ziaxp · May 31, 2015 at 08:19 PM

First put using UnityEngine.UI; then do the following and attach this script to UI TEXT Control.

      using UnityEngine;
   
      using UnityEngine.UI;

       using System.Collections;
 
      public class DisplayScore : MonoBehaviour {
 
  Text txt;
  private int currentscore=0;
 
  // Use this for initialization
  void Start () {
      txt = gameObject.GetComponent<Text>(); 
      txt.text="Score : " + currentscore;
  }
  
  // Update is called once per frame
  void Update () {
      txt.text="Score : " + currentscore;  
      currentscore = PlayerPrefs.GetInt("TOTALSCORE"); 
      PlayerPrefs.SetInt("SHOWSTARTSCORE",currentscore); 
  }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jacebook · Jan 22, 2018 at 06:29 AM 1
Share

I'm having an error with this one, it says "Cannot implicitly convert type 'int' to 'string'. I've copied your script exactly.

avatar image
0

Answer by Bunny83 · Apr 06, 2011 at 01:15 AM

Like Elliot said:

guiText.text = "New Text";

You didn't tell us what language you use, but fortunately this line is the same in JS or C# (i guess you don't use Boo? right?) ;)


edit

To change the GUIText of another GameObject you can put a public variable at the top of your script and drag the desired GameObject/GUIText onto the variable in the inspector.

// JS var targetGuiText : GUIText;

function Start() { targetGuiText.text = "Hey ya!"; }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Keavon · Apr 06, 2011 at 02:29 AM 0
Share

Hi. Sorry, yes I am using Javascript. Lol about Boo. I have never even heard of it before Unity. :P

avatar image
0

Answer by Danao · Sep 16, 2014 at 07:30 AM

Here what I used. I tried a few other methods that gave me some issues, this seems to work great with the yourText GUI Text Object dragged into the inspector:

 public class WhateverScript : MonoBehaviour {
 
     public GUIText yourText;
 
 void Update (){
 
     yourText.text = "Coins " + currentCoins + "/" + maxCoins;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling Text 1 Answer

Making a script for all gameobject GUITEXT. 1 Answer

Why this Error when trying to add a score using the new GUI ? 0 Answers

An object reference is required to access non-static member 2 Answers

How to find the origin point of a text line? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges