Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Changing GUI.Label text

Discussion in 'Extensions & OnGUI' started by shawnpigott, Jun 19, 2008.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I'm new to GUI stuff so please bear with me if this is very simple. I've been through the documentation and the forums but I can't figure out how to do this.

    I have a bunch of GUI.Buttons and a GUI.Label. I want to change the GUI.Label text depending on what button is pressed.

    How do I do this?

    I'm also curious how you access GUI stuff from external scripts. It seems like these two issues are related.
     
    vahid62 likes this.
  2. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    160
    you need to set the text content of a GUL.Label to a variable and then change that variable from an external script.

    e.g.
    Code (csharp):
    1. public var labelText : String = "This is some text";
    2.  
    3. function OnGUI() {
    4.     GUI.Label(Rect(10, 10, 140, 20), labelText);
    5. }
    then you just need to set the labelText variable from any where else in your code, such as by clicking a button.

    e.g.

    Code (csharp):
    1. function onGUI() {
    2.     if (GUI.Button(Rect(10, 40, 140, 20), "Change label")) {
    3.         labelText = "Different text";
    4.     }
    5. }
     
  3. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    Thanks.

    That worked great. Unfortunately it only seems to work from within the same script. Is there a way to change the value from another scrip?
     
  4. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    160
    no worries.

    yeah you can do that one of two ways. either get the value you want from another GameObject when you make the OnGUI call.

    e.g.

    Code (csharp):
    1. function OnGUI() {
    2.     var myObject : GameObject = GameObject.Find("anyGameObject");
    3.     var labelText : String = myObject.GetComponent(anyComponent).myVariable;
    4.     GUI.Label(Rect(10, 10, 140, 20), labelText);
    5. }
    or alter the variable in the component containing the OnGUI call externally.

    e.g.

    in the gui component (let's say this is a script called GUIManager in a GameObject called myGUIObject):
    Code (csharp):
    1. public var labelText : String = "This is some text";
    2.  
    3. function OnGUI() {
    4.     GUI.Label(Rect(10, 10, 140, 20), labelText);
    5. }
    and in any external script:
    Code (csharp):
    1. public function changeLabelText() : void {
    2.     var myObject : GameObject = GameObject.Find("myGUIObject");
    3.     myObject.GetComponent(GUIManager).labelText = "Different text";
    4. }
     
  5. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    That did the trick. Thanks for your help.