• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Trilby · Mar 06, 2014 at 06:33 AM · c#onguiunity4.3

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.

Comment

People who like this

0 Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fornoreason1000 · Mar 06, 2014 at 07:43 AM 0
Share

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 performing 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.

avatar image Trilby · Mar 06, 2014 at 07:55 AM 0
Share

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.

avatar image Fornoreason1000 · Mar 06, 2014 at 08:00 AM 0
Share

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 ();
                         }
                        }
avatar image Trilby · Mar 06, 2014 at 08:21 AM 0
Share

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 assuming that's right since that way I didn't get any errors.

avatar image GameVortex · Mar 06, 2014 at 09:02 AM 1
Share

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?

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

GUI texture outside of OnGUI 2 Answers

Making scrollbar for GUI.Label inside a draggable GUI.Window 0 Answers

Trying to use toggle as checkbox that only gets called once on click. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges