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 Boolean properly does change but is true check in Update() doesn't activate

Discussion in 'Editor & General Support' started by luketmcf, Feb 21, 2023.

  1. luketmcf

    luketmcf

    Joined:
    Nov 2, 2022
    Posts:
    2
    I've made a script with a boolean which allows the user to enable or disable data collection on startup. In theory this can also be toggled during runtime.

    On start up if the boolean is set to true then in runtime it's toggled to false, it does successfully start then stop the data collection. If the boolean is toggled to true, through a debug line it is evident that the boolean does get set back to true but the check which should be activated does not execute and I'm not sure why

    Here is the basis of the code:

    Code (CSharp):
    1.  
    2.  
    3. [SerializeField]
    4. private bool recordData; // user enabled in inspector
    5.  
    6. Update()
    7. {
    8.     Debug.Log("Record Data: " + recordData) ;
    9.     if(recordData)
    10.     {
    11.          //add data to list
    12.     }
    13. }
    14.  
    15.  
    16. public void toggleCollection()
    17. {
    18.     if(recordData)
    19.           recordData = false;
    20.     else
    21.           recordData = true;
    22. }
    I'm really not sure what's happening and was thinking it's something I don't understand with [SerializeField] but I've not run into this issue before.

    This has only been tested in preview, not in build - but I'm not convinced this matters (unless I'm very stupid)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    If the debug log line on line 8 is running and saying
    true
    then the if statement will run too. You didn't show what happens inside that
    if
    statement so any speculation on anything that happens inside there is futile.
     
  3. luketmcf

    luketmcf

    Joined:
    Nov 2, 2022
    Posts:
    2
    You're correct and i will make amends but a debug log inside the if statement does not execute