- Home /
GUILayout.Label is not displaying on level load
I have a script that is checking a line from a text file and then displaying the word using Labels. I call the function that gets the line in Start() and my GUILayout.Label is being run in OnGUI(). I also call the function to get a line on a button press in OnGUI(). Problem is, that the Layout will only display when I press the button, but not on the first check of OnGUI() after Start() runs. I have verified that the I am getting a valid word to display in Start(), so I have no idea why the Label is not displaying. my code follows:
function Start()
{
Debug.Log("Script has Started");
if(newgame == true)
{
newgame = false;
word = "";
GetWord();
}
}
function GetWord()
{
keyboardInstance.win = false;
keyboardInstance.played = false;
keyboardInstance.rightLetterCount = 0;
keyboardInstance.wrongCount = 0;
keyboardInstance.a = a;
keyboardInstance.b = b;
keyboardInstance.c = c;
keyboardInstance.d = d;
keyboardInstance.e = e;
keyboardInstance.f = f;
keyboardInstance.g = g;
keyboardInstance.h = h;
keyboardInstance.i = i;
keyboardInstance.j = j;
keyboardInstance.k = k;
keyboardInstance.l = l;
keyboardInstance.m = m;
keyboardInstance.n = n;
keyboardInstance.o = o;
keyboardInstance.p = p;
keyboardInstance.q = q;
keyboardInstance.r = r;
keyboardInstance.s = s;
keyboardInstance.t = t;
keyboardInstance.u = u;
keyboardInstance.v = v;
keyboardInstance.w = w;
keyboardInstance.x = x;
keyboardInstance.y = y;
keyboardInstance.z = z;
for (keyboardInstance.keyIndex = 0; keyboardInstance.keyIndex < 26; keyboardInstance.keyIndex++)
{
keyboardInstance.keyPressed[keyboardInstance.keyIndex] = false;
}
var sr : StreamReader = new StreamReader(path);
var fileContents = sr.ReadToEnd();
sr.Close();
var randomNumber = Random.Range(0, numWords);
var i : int = randomNumber;
var lines = fileContents.Split("\n"[0]);
for (line in lines[i]) {
word += line;
}
textureArray = new Texture[(word.length - 1)];
dashWidth = Screen.width/(word.length - 1);
for (index = 0; index < (word.length - 1); index++){
textureArray[index] = underscore;
}
activeWord = true;
Debug.Log(word);
}
function OnGUI () {
if (word != ""){
GUI.Label(Rect(Screen.width/2,Screen.height/2,70,70),textureArray[0]);
GUILayout.BeginArea(Rect(Screen.width/2 - ((word.Length - 1) * 70)/2,Screen.height - Screen.height/2,70 * (word.Length - 1),(Screen.height - Screen.height/2)/2));
GUILayout.BeginHorizontal();
for (counter = 0; counter < chars; counter++)
{
GUILayout.Label(textureArray[counter]);
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
activeWord = false;
}
if (GUI.Button(Rect(20,260,100,100),"",newGame)){
if (word != ""){
word = "";
GetWord();
}
Answer by DaveA · May 20, 2011 at 09:11 PM
Are you sure newgame is true before Start? Is it declared like var newgame = true; ?
Yes, I am setting newgame = true in the OnLevelWasLoaded() function, which is the first part of the script to run.
Yes, I have sent comments to the Debug.Log() and have confirmed the the script runs in the order of OnLevelWasLoaded() -> Awake() -> Start() -> OnGUI()
In Start you set word = ""; an in OnGUI, will only display the empty-string button if word == "" I would find another condition or set word = " " or something like that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081806im_/https://answers.unity.com/themes/thub/images/avi.jpg)