A script that ignores a bool check

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoinController : MonoBehaviour
{
    public GameObject[] coins;   // 8 coins. 1p, 2p, 5p, 10p, 20p, 50, £1, £2.
    public GameObject loseText;

    public int coinSelected;
    public int correctNumber;
    public int keyPressed = -1;
    public int points = 0;
    public int lives = 3;

    public Text scoreTextNumber;
    public Text Displaylives;

    public Text keyDisplayText1;
    public Text keyDisplayText2;
    public Text keyDisplayText3;
    public Text keyDisplayText4;
    public Text keyDisplayText5;
    public Text keyDisplayText6;
    public Text keyDisplayText7;
    public Text keyDisplayText8;

    private AudioSource audioSource;

    public AudioClip wrongChoice;
    public AudioClip correctChoice;

    public bool controlsActive = true;
    public bool someBool = false;

    public IEnumerator coinDisplayTime;

    void Start()
    {
        someBool = false;

        controlsActive = true;

        coinDisplayTime = WaitAndPrint(5f);
        StartCoroutine(coinDisplayTime);
        Reset();

        audioSource = GetComponent<AudioSource>();
    }

    private IEnumerator WaitAndPrint(float waitTime)
    {
        while (true)
        {
            yield return new WaitForSeconds(waitTime);
            Reset();
            coinSelected = Random.Range(0, coins.Length);
            correctNumber = coinSelected;
            coins[coinSelected].SetActive(true);
        }
    }

    void Update()
    {
        Displaylives.text = lives.ToString();

        if (controlsActive == true)
        {
            Controls();
            if (keyPressed == correctNumber) { coinAnswer = true; CorrectChoiceaudio(); Reset(); } // add before Reset someBool = false and maybe else {someBool = true;}
            //else { someBool = true; }

            scoreTextNumber.text = points.ToString();
        }
    }

    public void Controls()
    {
        if (Input.GetButtonDown("Key1"))
        {
            print("Key 1 has been pressed");
            keyPressed = 0;
            keyDisplayText1.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1.");
            LifeCounter();
        }

        if (Input.GetButtonDown("Key2"))
        {
            print("Key 2 has been pressed");
            keyPressed = 1;
            keyDisplayText2.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key3"))
        {
            print("Key 3 has been pressed");
            keyPressed = 2;
            keyDisplayText3.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key4"))
        {
            print("Key 4 has been pressed");
            keyPressed = 3;
            keyDisplayText4.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key5"))
        {
            print("Key 5 has been pressed");
            keyPressed = 4;
            keyDisplayText5.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key6"))
        {
            print("Key 6 has been pressed");
            keyPressed = 5;
            keyDisplayText6.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key7"))
        {
            print("Key 7 has been pressed");
            keyPressed = 6;
            keyDisplayText7.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }

        if (Input.GetButtonDown("Key8"))
        {
            print("Key 8 has been pressed");
            keyPressed = 7;
            keyDisplayText8.GetComponent<Text>().color = Color.green;
            WrongKeyaudio();
            print("Decrease lives by 1."); LifeCounter();
        }
        return; // Is this needed?
    }

    private void WrongKeyaudio()
    {
        audioSource.clip = wrongChoice;
        audioSource.Play();
        someBool = true;
    }

    private void CorrectChoiceaudio()
    {
        audioSource.clip = correctChoice;
        audioSource.Play();
        points++;
        print("increase points " + points);
    }

    void LifeCounter()
    {
        if (someBool == true) { Invoke ("Reset", 1.0f); stuff(); }
    }

    void stuff()
    {
        print("Reset points");
        points = 0;

        loseText.SetActive(true);
        controlsActive = false;
    }

    void Reset()
    {
        keyPressed = -1;
        coins[coinSelected].SetActive(false);
        someBool = false;

        keyDisplayText1.GetComponent<Text>().color = Color.black;
        keyDisplayText2.GetComponent<Text>().color = Color.black;
        keyDisplayText3.GetComponent<Text>().color = Color.black;
        keyDisplayText4.GetComponent<Text>().color = Color.black;
        keyDisplayText5.GetComponent<Text>().color = Color.black;
        keyDisplayText6.GetComponent<Text>().color = Color.black;
        keyDisplayText7.GetComponent<Text>().color = Color.black;
        keyDisplayText8.GetComponent<Text>().color = Color.black;
    }
}

I don’t think anyone is going to read all that.

Use code tags:

6 Likes

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

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.

Hey while(true) is a way to create infinity loop

OK then where exactly does it ignore the ==?

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

2 Likes

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.

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!

Yeah just catch what has been press in control and then see is it correct or not then go from there

I got it fixed. Thanks for the help. :slight_smile:
I can get drunk and rest easy now.