Variable does not keep value. It even switches between values.

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

Are you sure you don’t have more than one instance of this script in scene? If you can’t easily find them manually, you can use the context argument of Debug.Log function Unity - Scripting API: Debug.Log to help locating those objects.

3 Likes

Another approach to check if messages are coming from single or multiple objects is to print GetInstanceID

1 Like

Well, seeing consistent two false and one true in a loop is a pretty strong indicator that there are exactly 3 instances in the scene (active). Such confusion can be avoided by including the current frame count (Time.frameCount) in the log message. That way you can easily see which logs were generated in which frame. Though I would also suggest to add a context argument:

Debug.Log("#"+Time.frameCount + " userStoppedTimer: "+userStoppedTimer, gameObject);
2 Likes

Hey guys, thanks so much. Sorry, I am new to Unity and you were absolutely correct. I had several instances running.