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;
}
}