In the code below from the scripting docs I don’t see how to keep a reference to the label that gets created. I would like to be able to change the text of the label in an update function.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnGUI() {
GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
}
}
The way to think of it is as though “OnGUI” were being called every frame (very much like Update). In the body of the OnGUI you describe EVERY TIME what you want to appear for that frame which is being refreshed. As such, whatever is the string value used in GUI.Label for the frame where OnGUI is being called will be what appears on the display. There is no “object” being created for which a reference can be kept. Think of GUI.Label as being an instruction that says "For THIS frame, place the text label of ‘Hello World!’ at the specific coordinates.
Have a read of the docs again with this new notion in mind. I remember when it was first explained to me it took a while to be absorbed.