- Home /
Switch statement in If in GUI function
For learning purposes, I'm basically making a Taboo game version, I have a OnGUI function, with a if statement and a switch statement inside.
I have this code with a few Debug.Log to test it, everything works just except for the GUI.Label's showing up as supposed.` function OnGUI() {
GUI.Box(Rect(24, 29, 332, 372), "", boxStyle);
if(GUI.Button(new Rect(22,401, 50, 50),"Pass"))
{
Debug.Log("Test");
var randomWord = Random.Range(0, wordHeader.length);
var word = wordHeader[randomWord];
Debug.Log(word);
switch(word)
{
case "Carrot":
Debug.Log("c");
GUI.Label(Rect(0,38,Screen.width,Screen.height),"Carrot", labelStyle);
GUI.Label(Rect(0,160,Screen.width,Screen.height),"Orange", badWordsStyle);
GUI.Label(Rect(0,220,Screen.width,Screen.height),"Vegetable", badWordsStyle);
GUI.Label(Rect(0,280,Screen.width,Screen.height),"Bunny", badWordsStyle);
GUI.Label(Rect(0,340,Screen.width,Screen.height),"Crunchy", badWordsStyle);
break;'
However without the If statement, it seems to loop through the array every frame instead of just once, I think I have a solution for this part, but I can't figure out how to get the GUI.Label's to show with the if statement in place, all the Debug.Log's work fine, thanks in advance, any questions I'll be glad to answer
Sorry, but I'm not sure if I understand the question. Because OnGUI() is called every frame, you probably only want to select a new word on frames where the user has requested that (say, by hitting that button). Is your switch supposed to show only the current word? If so, you'll need a distinct case
for each word... though, if your words are already user-friendly, you might as well just draw the word directly, and skip the switch.
Oh of course, sorry I had a little moment there -.- I forgot OnGUI was every frame. Thanks for the quick reply :) I'll change my code around :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do you use Unity Remote on an Android? 0 Answers
Update unity can't find old projects 1 Answer
unty wont work! 3 Answers