Hi guys,
I am going crazy on that one. I initialize the variable “userStoppedTimer” and set it to false. When the user clicks the button (“bomb”) the variable should be set to true. But instead of staying true it keeps going back and forth between true and false.
Any ideas about what is happening here?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class NewTestScript : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] public TextMeshProUGUI timerText;
[SerializeField] private Button bombButton;
[SerializeField] private Sprite clockImage;
public Sprite redButtonImage;
private int timeUntilExplosion = 0;
private float timer = 0;
bool bombExploded = true;
private bool userStoppedTimer = false;
private const int minimumTime = 10;
private const int maximumTime = 15;
void Start()
{
timerText = this.GetComponent<TextMeshProUGUI>();
timerText.text = "00:00";
timeUntilExplosion = Random.Range(minimumTime,maximumTime);
Debug.Log("TimeUntilExplosion: "+timeUntilExplosion);
bombExploded = false;
}
// Update is called once per frame
void Update()
{
Debug.Log("userStoppedTimer: "+userStoppedTimer);
if (!bombExploded) {
if (timer <= timeUntilExplosion) {
timer += Time.deltaTime;
this.DisplayTimer(timer);
} else {
if (userStoppedTimer) {
} else {
Debug.Log("BOOOOM");
bombExploded = true;
this.changeText("BOOOOM");
bombButton.image.sprite = redButtonImage;
}
}
}
}
void changeText(string textChangeText) {
Debug.Log("Starting ChangeText Method");
timerText.text = textChangeText;
}
void DisplayTimer(float timeToDisplay) {
if(!userStoppedTimer) {
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
public void StopTheBombTimer() {
Debug.Log ("You have clicked the bomb!");
userStoppedTimer = true;
Debug.Log (userStoppedTimer);
}
}
I appreciate every help.
Best
Markus