Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

A script that ignores a bool check

Discussion in 'Scripting' started by Rockyeahh, Aug 2, 2020.

  1. Rockyeahh

    Rockyeahh

    Joined:
    May 19, 2016
    Posts:
    17
    Here is an odd one for ya. A script that ignores my bool == true and/or false. For whatever reason it just moves ahead as if the bool is meaningless. It should set someBool = true inside the WrongChoiceAudio method but it sets it to true if the CorrectChoiceAudio is called.
    If anyone feels like a headscratcher then here's the code.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CoinController : MonoBehaviour
    7. {
    8.     public GameObject[] coins;   // 8 coins. 1p, 2p, 5p, 10p, 20p, 50, £1, £2.
    9.     public GameObject loseText;
    10.  
    11.     public int coinSelected;
    12.     public int correctNumber;
    13.     public int keyPressed = -1;
    14.     public int points = 0;
    15.     public int lives = 3;
    16.  
    17.     public Text scoreTextNumber;
    18.     public Text Displaylives;
    19.  
    20.     public Text keyDisplayText1;
    21.     public Text keyDisplayText2;
    22.     public Text keyDisplayText3;
    23.     public Text keyDisplayText4;
    24.     public Text keyDisplayText5;
    25.     public Text keyDisplayText6;
    26.     public Text keyDisplayText7;
    27.     public Text keyDisplayText8;
    28.  
    29.     private AudioSource audioSource;
    30.  
    31.     public AudioClip wrongChoice;
    32.     public AudioClip correctChoice;
    33.  
    34.     public bool controlsActive = true;
    35.     public bool someBool = false;
    36.  
    37.     public IEnumerator coinDisplayTime;
    38.  
    39.     void Start()
    40.     {
    41.         someBool = false;
    42.  
    43.         controlsActive = true;
    44.  
    45.         coinDisplayTime = WaitAndPrint(5f);
    46.         StartCoroutine(coinDisplayTime);
    47.         Reset();
    48.  
    49.         audioSource = GetComponent<AudioSource>();
    50.     }
    51.  
    52.     private IEnumerator WaitAndPrint(float waitTime)
    53.     {
    54.         while (true)
    55.         {
    56.             yield return new WaitForSeconds(waitTime);
    57.             Reset();
    58.             coinSelected = Random.Range(0, coins.Length);
    59.             correctNumber = coinSelected;
    60.             coins[coinSelected].SetActive(true);
    61.         }
    62.     }
    63.  
    64.     void Update()
    65.     {
    66.         Displaylives.text = lives.ToString();
    67.  
    68.         if (controlsActive == true)
    69.         {
    70.             Controls();
    71.             if (keyPressed == correctNumber) { coinAnswer = true; CorrectChoiceaudio(); Reset(); } // add before Reset someBool = false and maybe else {someBool = true;}
    72.             //else { someBool = true; }
    73.  
    74.             scoreTextNumber.text = points.ToString();
    75.         }
    76.     }
    77.  
    78.     public void Controls()
    79.     {
    80.         if (Input.GetButtonDown("Key1"))
    81.         {
    82.             print("Key 1 has been pressed");
    83.             keyPressed = 0;
    84.             keyDisplayText1.GetComponent<Text>().color = Color.green;
    85.             WrongKeyaudio();
    86.             print("Decrease lives by 1.");
    87.             LifeCounter();
    88.         }
    89.  
    90.         if (Input.GetButtonDown("Key2"))
    91.         {
    92.             print("Key 2 has been pressed");
    93.             keyPressed = 1;
    94.             keyDisplayText2.GetComponent<Text>().color = Color.green;
    95.             WrongKeyaudio();
    96.             print("Decrease lives by 1."); LifeCounter();
    97.         }
    98.  
    99.         if (Input.GetButtonDown("Key3"))
    100.         {
    101.             print("Key 3 has been pressed");
    102.             keyPressed = 2;
    103.             keyDisplayText3.GetComponent<Text>().color = Color.green;
    104.             WrongKeyaudio();
    105.             print("Decrease lives by 1."); LifeCounter();
    106.         }
    107.  
    108.         if (Input.GetButtonDown("Key4"))
    109.         {
    110.             print("Key 4 has been pressed");
    111.             keyPressed = 3;
    112.             keyDisplayText4.GetComponent<Text>().color = Color.green;
    113.             WrongKeyaudio();
    114.             print("Decrease lives by 1."); LifeCounter();
    115.         }
    116.  
    117.         if (Input.GetButtonDown("Key5"))
    118.         {
    119.             print("Key 5 has been pressed");
    120.             keyPressed = 4;
    121.             keyDisplayText5.GetComponent<Text>().color = Color.green;
    122.             WrongKeyaudio();
    123.             print("Decrease lives by 1."); LifeCounter();
    124.         }
    125.  
    126.         if (Input.GetButtonDown("Key6"))
    127.         {
    128.             print("Key 6 has been pressed");
    129.             keyPressed = 5;
    130.             keyDisplayText6.GetComponent<Text>().color = Color.green;
    131.             WrongKeyaudio();
    132.             print("Decrease lives by 1."); LifeCounter();
    133.         }
    134.  
    135.         if (Input.GetButtonDown("Key7"))
    136.         {
    137.             print("Key 7 has been pressed");
    138.             keyPressed = 6;
    139.             keyDisplayText7.GetComponent<Text>().color = Color.green;
    140.             WrongKeyaudio();
    141.             print("Decrease lives by 1."); LifeCounter();
    142.         }
    143.  
    144.         if (Input.GetButtonDown("Key8"))
    145.         {
    146.             print("Key 8 has been pressed");
    147.             keyPressed = 7;
    148.             keyDisplayText8.GetComponent<Text>().color = Color.green;
    149.             WrongKeyaudio();
    150.             print("Decrease lives by 1."); LifeCounter();
    151.         }
    152.         return; // Is this needed?
    153.     }
    154.  
    155.     private void WrongKeyaudio()
    156.     {
    157.         audioSource.clip = wrongChoice;
    158.         audioSource.Play();
    159.         someBool = true;
    160.     }
    161.  
    162.     private void CorrectChoiceaudio()
    163.     {
    164.         audioSource.clip = correctChoice;
    165.         audioSource.Play();
    166.         points++;
    167.         print("increase points " + points);
    168.     }
    169.  
    170.     void LifeCounter()
    171.     {
    172.         if (someBool == true) { Invoke ("Reset", 1.0f); stuff(); }
    173.     }
    174.  
    175.     void stuff()
    176.     {
    177.         print("Reset points");
    178.         points = 0;
    179.  
    180.         loseText.SetActive(true);
    181.         controlsActive = false;
    182.     }
    183.  
    184.     void Reset()
    185.     {
    186.         keyPressed = -1;
    187.         coins[coinSelected].SetActive(false);
    188.         someBool = false;
    189.  
    190.         keyDisplayText1.GetComponent<Text>().color = Color.black;
    191.         keyDisplayText2.GetComponent<Text>().color = Color.black;
    192.         keyDisplayText3.GetComponent<Text>().color = Color.black;
    193.         keyDisplayText4.GetComponent<Text>().color = Color.black;
    194.         keyDisplayText5.GetComponent<Text>().color = Color.black;
    195.         keyDisplayText6.GetComponent<Text>().color = Color.black;
    196.         keyDisplayText7.GetComponent<Text>().color = Color.black;
    197.         keyDisplayText8.GetComponent<Text>().color = Color.black;
    198.     }
    199. }
     
    Last edited: Aug 7, 2020 at 2:29 PM
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,521
  3. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    163
    yep delete all your irrelevant code and if the bug still persists you can post only the problematic segment

    if its as simple as you describe then you should post a simple piece of code aswell
     
  4. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    43
    I can see two problems 1. you didn't use code brackets and second how is the computer supposed to know what you mean with while(true) in the Coroutine WaitAndPrint because there is no bool named true.
     
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    151
    Hey while(true) is a way to create infinity loop
     
  6. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    43
    OK then where exactly does it ignore the ==?
     
  7. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    151
    Problem is in his logic .. he call wrongkey no matter what he press and life counter and then if correct then correct key so no matter what it will be true even correct being call..
    So life counter will be always ==true because somebool is always true before correct has call

    So no matter what is press the correct number or not the wrongkey is always called before so somebool always be true and the bool check in lifecounter will always go through

    So I think that's where he think it got ignore but the true is just the logic problem so stuff() always got called and point will alwasy 0 even he press the correct number
     
    Last edited: Aug 3, 2020 at 1:43 PM
    Bunny83 and Rockyeahh like this.
  8. Rockyeahh

    Rockyeahh

    Joined:
    May 19, 2016
    Posts:
    17
    Ah cool. It's been years since I posted here and didn't remember about the tools used for posting code. I actually forgot that I posted this so sorry for the late reply. lol.
     
    Last edited: Aug 7, 2020 at 2:38 PM
  9. Rockyeahh

    Rockyeahh

    Joined:
    May 19, 2016
    Posts:
    17
    Hm, yes that's got to be it. I was thinking that I'd need to change the way in which correct number and wrong key are called.
    Although point does increase by 1 if I press the correct number.
    I do find it funny that I must have reached such a level of defeat that I called a method stuff. Astoundingly lazy of me!
     
    Last edited: Aug 7, 2020 at 2:41 PM
  10. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    151
    Yeah just catch what has been press in control and then see is it correct or not then go from there
     
  11. Rockyeahh

    Rockyeahh

    Joined:
    May 19, 2016
    Posts:
    17
    I got it fixed. Thanks for the help. :)
    I can get drunk and rest easy now.
     
unityunity