Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

changing font + size of text 1 Answer

HELP with scripting where GUI is involved 2 Answers

How to make text damage notifiers above enemy 1 Answer

GUIText not working properly with a timer. 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges