Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by MrProudburg, Jun 15, 2022.

  1. MrProudburg

    MrProudburg

    Joined:
    Jun 8, 2022
    Posts:
    2
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class NewTestScript : MonoBehaviour
    8. {
    9.     // Start is called before the first frame update
    10.  
    11.     [SerializeField] public TextMeshProUGUI timerText;
    12.     [SerializeField] private Button bombButton;
    13.     [SerializeField] private Sprite clockImage;
    14.     public Sprite redButtonImage;
    15.     private int timeUntilExplosion = 0;
    16.     private float timer = 0;
    17.     bool bombExploded = true;
    18.     private bool userStoppedTimer = false;
    19.     private const int minimumTime = 10;
    20.     private const int maximumTime = 15;
    21.  
    22.     void Start()
    23.     {
    24.         timerText = this.GetComponent<TextMeshProUGUI>();
    25.         timerText.text = "00:00";
    26.         timeUntilExplosion = Random.Range(minimumTime,maximumTime);
    27.         Debug.Log("TimeUntilExplosion: "+timeUntilExplosion);
    28.         bombExploded = false;
    29.        
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         Debug.Log("userStoppedTimer: "+userStoppedTimer);
    36.  
    37.         if (!bombExploded) {
    38.             if (timer <= timeUntilExplosion) {
    39.                 timer += Time.deltaTime;
    40.                 this.DisplayTimer(timer);
    41.             } else {
    42.                 if (userStoppedTimer) {
    43.  
    44.                 } else {
    45.                     Debug.Log("BOOOOM");
    46.                     bombExploded = true;
    47.                     this.changeText("BOOOOM");
    48.                     bombButton.image.sprite = redButtonImage;
    49.                 }
    50.             }
    51.         }
    52.  
    53.  
    54.  
    55.        
    56.     }
    57.  
    58.     void changeText(string textChangeText) {
    59.         Debug.Log("Starting ChangeText Method");
    60.         timerText.text = textChangeText;
    61.     }
    62.  
    63.     void DisplayTimer(float timeToDisplay) {
    64.         if(!userStoppedTimer) {
    65.             float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    66.             float seconds = Mathf.FloorToInt(timeToDisplay % 60);
    67.             timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);          
    68.         }
    69.  
    70.     }
    71.  
    72.     public void StopTheBombTimer() {
    73.         Debug.Log ("You have clicked the bomb!");
    74.         userStoppedTimer = true;
    75.         Debug.Log (userStoppedTimer);
    76.     }
    77. }
    78.  


    I appreciate every help.

    Best
    Markus
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    MrProudburg, Bunny83 and Kurt-Dekker like this.
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Another approach to check if messages are coming from single or multiple objects is to print GetInstanceID
     
    MrProudburg likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    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:

    Code (CSharp):
    1. Debug.Log("#"+Time.frameCount + " userStoppedTimer: "+userStoppedTimer, gameObject);
     
    MrProudburg and mopthrow like this.
  5. MrProudburg

    MrProudburg

    Joined:
    Jun 8, 2022
    Posts:
    2
    Hey guys, thanks so much. Sorry, I am new to Unity and you were absolutely correct. I had several instances running.