- Home /
All answers returning true for OnGUI question
Hello again all :)
In my game, I have a switch statement being used to generate a question inside an OnGUI function, and to declare which answer is correct.
switch (questionNum)
{
case 1:
GUI.Box(new Rect((Screen.width / 2.0f)-75.0f, 200, 300, 30), "What capturable point in Dawn of War allows you to build super units?");
if (GUI.Button(new Rect((Screen.width / 2.0f)-75.0f, 230, 50, 30), "Relic"))
{
correct = true;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)-25.0f, 230, 50, 30), "Strategic Point"))
{
correct = false;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)+25.0f, 230, 50, 30), "Slag Deposit"))
{
correct = false;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)+75.0f, 230, 50, 30), "Critical Location"))
{
correct = false;
ask = false;
}
break;
}
asked = true;
}
In the game manager, I have a script to call the question when the player lands on a yellow square. If the player answers correctly, they get to roll the die again.
using UnityEngine;
using System.Collections;
public class YellowSpaceScript : SpaceScript
{
private int questionNum;
public static bool correct = false;
public static bool ask = false;
public static bool asked = true;
public override void spaceRules()
{
Debug.Log("Ask a question on RTS games.");
// pick a random number between 1 and 20
//questionNum = Random.Range(1,21);
questionNum = 1;
ask = true;
asked = false;
}
void OnGUI()
{
if (ask == true)
{
// use the random number to generate a question
switch (questionNum)
{
case 1:
GUI.Box(new Rect((Screen.width / 2.0f)-75.0f, 200, 300, 30), "What capturable point in Dawn of War allows you to build super units?");
if (GUI.Button(new Rect((Screen.width / 2.0f)-75.0f, 230, 50, 30), "Relic"))
{
correct = true;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)-25.0f, 230, 50, 30), "Strategic Point"))
{
correct = false;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)+25.0f, 230, 50, 30), "Slag Deposit"))
{
correct = false;
ask = false;
}
if (GUI.Button(new Rect((Screen.width / 2.0f)+75.0f, 230, 50, 30), "Critical Location"))
{
correct = false;
ask = false;
}
break;
}
Debug.Log(correct);
asked = true;
Debug.Log(correct);
}
}
}
The scripts all work fine, aside from one, rather important, detail. Whenever an incorrect answer is clicked, the player is still able to roll again, with control not passing over to the next player. However, on all non-yellow squares (which are currently just empty spaces), the nextPlayer() function works fine. I'm assuming the problem lies in the switch statement, but I can't see what might be causing the issue.
Have you tired using debug statements? They are very useful for logic flaws in scripts. we can use them to find out exactly whats happening to the script as its start perfor$$anonymous$$g its task.
Debug.Log(correct);
put this in your buttons and before and after you checked for "asked" to be true. see what it does. you could also check if the check for "correct" to be true is called .
by the sounds of your problem your check for being correct is being either ignored(asked is false), bypassed(gets changed before being checked) or is being done at the wrong point of time and when it gets to check, correct is true (this can happen in hundreds of ways).
Your switch statement looks correct to me (i even tested it). so i honestly do not how to help you here.
Just tried, got:
False
UnityEngine.Debug:Log(Object)
On both before and after 'asked' is checked in the buttons.
Edit: This is when I chose a wrong answer.
Good now you know that is working now check this. and any mention of correct in your scripts. your trying to find at what point it changes to true
if (YellowSpaceScript.asked == true)
{
Debug.Log(correct);
if (YellowSpaceScript.correct == true)
{
diceEnabled = true;
YellowSpaceScript.correct = false;
}
// if the player answers incorrectly, the next player takes their go
else
{
nextPlayer ();
}
}
Those are the only two places correct is mentioned, besides where it is declared (and I set it to false by default), and I don't recieve any debug notes from the main script (the one you just told me to add the log to), just the two in the YellowSpaceScript file repeated about 330 times each.
Edit: I set the Debug.Log to (YellowSpaceScript.correct), I'm assu$$anonymous$$g that's right since that way I didn't get any errors.
If that Debug is not printing out it means that asked in the YellowSpaceScript is not set to true. You set it to true right after the switch statement, maybe something else is setting it to false?
Your answer
![](https://koobas.hobune.stream/wayback/20220613135505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I press ESC once, Unity thinks I've pressed it many times... 2 Answers
How to make OnGUI Texture able to be clicked 1 Answer
How to remove GUILayer? 2 Answers