Search Unity

Boolean not changing properly C#

Discussion in 'Scripting' started by Vedvart1, Nov 29, 2015.

  1. Vedvart1

    Vedvart1

    Joined:
    Nov 8, 2015
    Posts:
    2
    I have this script which controls 4 answer boxes and a question box for a trivia game. The verifyForButtonX() methods are for the UI Buttons to use as onClick() events. the problem arises when one fo the buttons is triggered, it runs either displayRight() or displayWrong() and it should set the boolean questionAnswered to true, but it seemingly doesn't. I've tried restarting unity to no avail, and i can't see why it doesn't set properly. It does run the rest of the method as the sound plays and the colors change properly. Here is the code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class QuestionController : MonoBehaviour {
    6.  
    7.     private GameObject canvas;
    8.     private static UnityEngine.UI.Text Answer1;
    9.     private static UnityEngine.UI.Text Answer2;
    10.     private static UnityEngine.UI.Text Answer3;
    11.     private static UnityEngine.UI.Text Answer4;
    12.     private static UnityEngine.UI.Text Question;
    13.  
    14.     private static bool enableQuestion;
    15.     public static bool questionAnswered;
    16.     public static bool questionAnsweredCorrectly;
    17.     private static int correctAnswer;
    18.  
    19.     public AudioClip RightSound;
    20.     public AudioClip WrongSound;
    21.  
    22.     private static UnityEngine.UI.Button Answer1Box;
    23.     private static UnityEngine.UI.Button Answer2Box;
    24.     private static UnityEngine.UI.Button Answer3Box;
    25.     private static UnityEngine.UI.Button Answer4Box;
    26.     private static UnityEngine.UI.Image QuestionBox;
    27.  
    28.     private static UnityEngine.UI.Button CorrectBox;
    29.  
    30.  
    31.     //Called on initialization
    32.     void Start () {
    33.  
    34.         enableQuestion = false;
    35.         questionAnsweredCorrectly = false;
    36.  
    37.         Answer1 = GameObject.FindGameObjectWithTag("Answer1Box").GetComponentInChildren<UnityEngine.UI.Text>();
    38.         Answer2 = GameObject.FindGameObjectWithTag("Answer2Box").GetComponentInChildren<UnityEngine.UI.Text>();
    39.         Answer3 = GameObject.FindGameObjectWithTag("Answer3Box").GetComponentInChildren<UnityEngine.UI.Text>();
    40.         Answer4 = GameObject.FindGameObjectWithTag("Answer4Box").GetComponentInChildren<UnityEngine.UI.Text>();
    41.         Question = GameObject.FindGameObjectWithTag("QuestionBox").GetComponentInChildren<UnityEngine.UI.Text>();
    42.  
    43.         Answer1Box = GameObject.FindGameObjectWithTag("Answer1Box").GetComponent<UnityEngine.UI.Button>();
    44.         Answer2Box = GameObject.FindGameObjectWithTag("Answer2Box").GetComponent<UnityEngine.UI.Button>();
    45.         Answer3Box = GameObject.FindGameObjectWithTag("Answer3Box").GetComponent<UnityEngine.UI.Button>();
    46.         Answer4Box = GameObject.FindGameObjectWithTag("Answer4Box").GetComponent<UnityEngine.UI.Button>();
    47.         QuestionBox = GameObject.FindGameObjectWithTag("QuestionBox").GetComponent<UnityEngine.UI.Image>();
    48.  
    49.         CorrectBox = Answer1Box;
    50.  
    51.  
    52.         clearQuestion();
    53.  
    54.     }
    55.  
    56.     void OnGUI() {
    57.        
    58.         if(enableQuestion && !questionAnswered)
    59.         {
    60.  
    61.             if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.Alpha1) || Input.GetKeyUp(KeyCode.Keypad1))
    62.             {
    63.                 verifyAnswer('a', Answer1Box);
    64.             } else if (Input.GetKeyUp(KeyCode.B) || Input.GetKeyUp(KeyCode.Alpha2) || Input.GetKeyUp(KeyCode.Keypad2))
    65.             {
    66.                 verifyAnswer('b', Answer2Box);
    67.             }
    68.             else if (Input.GetKeyUp(KeyCode.C) || Input.GetKeyUp(KeyCode.Alpha3) || Input.GetKeyUp(KeyCode.Keypad3))
    69.             {
    70.                 verifyAnswer('c', Answer3Box);
    71.             }
    72.             else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.Alpha4) || Input.GetKeyUp(KeyCode.Keypad4))
    73.             {
    74.                 verifyAnswer('d', Answer4Box);
    75.             }
    76.         }
    77.  
    78.     }
    79.  
    80.     public static void setQuestionValues(string question, string answer1, string answer2, string answer3, string answer4, int correctAnswerLocal)
    81.     {
    82.         Answer1.text = "A) " + answer1;
    83.         Answer2.text = "B) " + answer2;
    84.         Answer3.text = "C) " + answer3;
    85.         Answer4.text = "D) " + answer4;
    86.         Question.text = question;
    87.         correctAnswer = correctAnswerLocal;
    88.  
    89.         questionAnswered = false;
    90.         setTransparency(Answer1Box, 1);
    91.         setTransparency(Answer2Box, 1);
    92.         setTransparency(Answer3Box, 1);
    93.         setTransparency(Answer4Box, 1);
    94.  
    95.         switch (correctAnswer)
    96.         {
    97.             case 1:
    98.                 CorrectBox = Answer1Box;
    99.                 break;
    100.             case 2:
    101.                 CorrectBox = Answer2Box;
    102.                 break;
    103.             case 3:
    104.                 CorrectBox = Answer3Box;
    105.                 break;
    106.             case 4:
    107.                 CorrectBox = Answer4Box;
    108.                 break;
    109.         }
    110.  
    111.  
    112.         Answer1Box.enabled = true;
    113.         Answer2Box.enabled = true;
    114.         Answer3Box.enabled = true;
    115.         Answer4Box.enabled = true;
    116.         QuestionBox.enabled = true;
    117.  
    118.         enableQuestion = true;
    119.     }
    120.     public static void clearQuestion()
    121.     {
    122.         changeColor(Answer1Box, Color.white, new Color(245, 245, 245, 1), new Color(200, 200, 200, 1));
    123.         changeColor(Answer2Box, Color.white, new Color(245, 245, 245, 1), new Color(200, 200, 200, 1));
    124.         changeColor(Answer3Box, Color.white, new Color(245, 245, 245, 1), new Color(200, 200, 200, 1));
    125.         changeColor(Answer4Box, Color.white, new Color(245, 245, 245, 1), new Color(200, 200, 200, 1));
    126.  
    127.         setTransparency(Answer1Box, 0);
    128.         setTransparency(Answer2Box, 0);
    129.         setTransparency(Answer3Box, 0);
    130.         setTransparency(Answer4Box, 0);
    131.  
    132.         Answer1.text = "";
    133.         Answer2.text = "";
    134.         Answer3.text = "";
    135.         Answer4.text = "";
    136.         Question.text = "";
    137.  
    138.         QuestionBox.enabled = false;
    139.         questionAnswered = true;
    140.     }
    141.  
    142.     public void displayWrong(UnityEngine.UI.Button SelectedBox)
    143.     {
    144.         if (!questionAnswered)
    145.         {
    146.             SelectedBox.image.color = Color.red;
    147.             CorrectBox.image.color = Color.green;
    148.             questionAnswered = true;
    149.             questionAnsweredCorrectly = false;
    150.  
    151.             AudioSource.PlayClipAtPoint(WrongSound, GameObject.FindGameObjectWithTag("MainCamera").transform.position, 1f);
    152.  
    153.             changeColor(Answer1Box, Color.white, Color.white, Color.white);
    154.             changeColor(Answer2Box, Color.white, Color.white, Color.white);
    155.             changeColor(Answer3Box, Color.white, Color.white, Color.white);
    156.             changeColor(Answer4Box, Color.white, Color.white, Color.white);
    157.         }
    158.     }
    159.     public void displayRight()
    160.     {
    161.         if (!questionAnswered)
    162.         {
    163.             CorrectBox.image.color = Color.green;
    164.             questionAnswered = true;
    165.             questionAnsweredCorrectly = true;
    166.  
    167.             AudioSource.PlayClipAtPoint(RightSound, GameObject.FindGameObjectWithTag("MainCamera").transform.position, 1f);
    168.  
    169.             changeColor(Answer1Box, Color.white, Color.white, Color.white);
    170.             changeColor(Answer2Box, Color.white, Color.white, Color.white);
    171.             changeColor(Answer3Box, Color.white, Color.white, Color.white);
    172.             changeColor(Answer4Box, Color.white, Color.white, Color.white);
    173.         }
    174.     }
    175.  
    176.  
    177.     public void verifyAnswer(char answer, UnityEngine.UI.Button SelectedBox)
    178.     {
    179.         switch(answer)
    180.         {
    181.             case 'a':
    182.                 if(correctAnswer == 1)
    183.                 {
    184.                     displayRight();
    185.                 } else
    186.                 {
    187.                     displayWrong(SelectedBox);
    188.                 }
    189.                 break;
    190.             case 'b':
    191.                 if (correctAnswer == 2)
    192.                 {
    193.                     displayRight();
    194.                 }
    195.                 else
    196.                 {
    197.                     displayWrong(SelectedBox);
    198.                 }
    199.                 break;
    200.             case 'c':
    201.                 if (correctAnswer == 3)
    202.                 {
    203.                     displayRight();
    204.                 }
    205.                 else
    206.                 {
    207.                     displayWrong(SelectedBox);
    208.                 }
    209.                 break;
    210.             case 'd':
    211.                 if (correctAnswer == 4)
    212.                 {
    213.                     displayRight();
    214.                 }
    215.                 else
    216.                 {
    217.                     displayWrong(SelectedBox);
    218.                 }
    219.                 break;
    220.         }
    221.     }
    222.  
    223.     public void verifyForButton1()
    224.     {
    225.         verifyAnswer('a', Answer1Box);
    226.     }
    227.     public void verifyForButton2()
    228.     {
    229.         verifyAnswer('b', Answer2Box);
    230.     }
    231.     public void verifyForButton3()
    232.     {
    233.         verifyAnswer('c', Answer3Box);
    234.     }
    235.     public void verifyForButton4()
    236.     {
    237.         verifyAnswer('d', Answer4Box);
    238.     }
    239.  
    240.     public static void changeColor(UnityEngine.UI.Button button, Color normalColor, Color highlightedColor, Color pressedColor)
    241.     {
    242.         button = button.GetComponent<UnityEngine.UI.Button>();
    243.         UnityEngine.UI.ColorBlock colorBlock = button.colors;
    244.         colorBlock.normalColor = normalColor;
    245.         colorBlock.highlightedColor = highlightedColor;
    246.         colorBlock.pressedColor = pressedColor;
    247.         button.colors = colorBlock;
    248.     }
    249.     public static void setTransparency(UnityEngine.UI.Button button, int transparency)
    250.     {
    251.         changeColor(button, new Color(button.colors.normalColor.r, button.colors.normalColor.g, button.colors.normalColor.b, transparency),
    252.                                 new Color(button.colors.highlightedColor.r, button.colors.highlightedColor.g, button.colors.highlightedColor.b, transparency),
    253.                                 new Color(button.colors.pressedColor.r, button.colors.pressedColor.g, button.colors.pressedColor.b, transparency));
    254.     }
    255.  
    256. }
    257.  
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    If you're seeing the results of the displayWrong() or displayRight() methods run, but the variable is still false, then I would suspect that something else is changing the value of the variable.

    There are two things to try in order to track down the culprit. One is easy, and the other involves slightly more work but is useful in more general cases.

    The easy way:
    Change quiestionAnswered in to a Property, and put a Debug.Log message in the setter so that you can see exactly where and when it is being changed.
    Code (csharp):
    1.  
    2. // instead of this
    3. public static bool questionAnswered;
    4. // have this
    5. private static bool _questionAnswered;
    6. public static bool questionAnswered
    7. {
    8. get
    9. {
    10. return _questionAnswered;
    11. }
    12. set
    13. {
    14. Debug.Log("Setting questionAnswered to " + value);
    15. _questionAnswered = value;
    16. }
    17. }
    18.  
    This will cause a call stack to appear in the console whenever a method changes the value. You can see who is changing it and when it is occurring.

    The harder way is to set a breakpoint at each of the places where questionAnswered has its value changed. Then attach the debugger to Unity, and step through the execution of the program. See https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger
     
  3. Vedvart1

    Vedvart1

    Joined:
    Nov 8, 2015
    Posts:
    2
    Thanks, turned out i had been calling setQuestionValues() in an if statement in Update() but wasn't considering if the question was already displayed or not, so it was calling that over and over.