How to reference a GUI object

Okay, So this is my first time posting on the forums. I’ve been reading through the the pages, trying to find something that would be helpful for me in understanding the Unity GUI, but it’s just not clicking.

I come from a strong background in Visual Basic (versions 3-6) and the GUI elements make sense, but confuse me at the same time.

I’m used to referencing objects by names and handles, so is there an efficient way to continue doing this with the Unity GUI?

Here’s a sample of code:

function DoScoreWindow (windowsID : int) {
    //Make the window be draggble in the top 20 pixels
    GUI.DragWindow(Rect(0,0,System.Decimal.MaxValue, 20));


    GUILayout.Label("My Score for Level 1 is:");
    var lblStar : int = GUILayout.Label(MYSTARS+"");
    var lblCrate : int = GUILayout.Label(MYCRATES+"");
    lblStar.name = "lblStarCounter";
    lblCrate.name = "lblCrateCounter";
}

This is an edited copy from the 2D Platform tutorial on the site.

MYSTARS is a static variable updated with the OnTrigger Event in another script, essentially it’s a counter variable.

MYCRATES is also a static variable that’s updated through another OnTrigger Event.

I’m using Debug.Log(varName+“”); and the variables are updating correctly. Just having a problem working with Unity GUI. I’ve read through the information available on the main website, but the connection just isn’t being made mentally.

As you can see, the code written above doesn’t work! How would I save the handle of the GUI item, so it can be (1)named, and then the (2)text can be updated through other scripts in the project?

My thought process is geared more towards VB, so any one who can explain it simply would be appreciated!

-Tony Z.

Is there a way to make reference to GUI objects when creating them in this fashion? If I create the GUI object through the components menu, I can reference the object through there, but I’m just more or less curious about doing this through the code aspect to try and understand it better.

You cannot store a reference to any GUI. or GUILayout. object; they don’t exist like that. However, you can make a label which displays a variable and edit that variable instead quite easily:

string _labelText;

public string LabelText{
get{ return _labelText;}
set{ _labelText = value;
}

void OnGUI(){
     GUILayout.Label(_labelText);
}

void Update(){
_labelText = Time.time.ToString();
}

The OnGUI code displays information; it does not store information nor does it have any sort of memory (and altering values in OnGUI code is very error-prone- any alterations of the values should be handled external to OnGUI, like in the Update loop for example - aside from elements designed specifically for user input like TextArea of course). Don’t consider the GUI elements as objects and you’ll be fine.

Well… with the knowledge that using the GUI my was incorrect, I just went back to adding GUI components through the Menu system and everything works again. Just trying out different things to learn as much about Unity as I can through hands on experiences. :smile:

Thank you for your time!