Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Changing GUI.Label text

Discussion in 'Immediate Mode GUI (IMGUI)' 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:
    167
    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:
    167
    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.