Search Unity

Resolved Boolean Value Not Changing

Discussion in 'Scripting' started by Norteous3, May 29, 2023.

  1. Norteous3

    Norteous3

    Joined:
    Jan 2, 2023
    Posts:
    16
    I have been working on a game in Unity 2021.3.17f1 and I have a boolean value that refuses to change. The odd part is I used some code that is nearly the same and that works. I know that the code is being triggered because I tested it with Debug.Log(). I don't know if I am simply making a stupid mistake or something else. If anyone knows the solution to this it would be greatly appreciated.

    Here is the code:

    Code (CSharp):
    1.     public void PowerToggle()
    2.     {
    3.         if (Power == true)
    4.         {
    5.             Power = false;
    6.         }
    7.         else {
    8.             Power = true;
    9.         }
    10.     }
    11.  
    12.     public void CoolantToggle()
    13.     {
    14.         if (CoolantOn == true)
    15.         {
    16.             CoolantOn = false;
    17.         }
    18.         else {
    19.             CoolantOn = true;
    20.         }
    21.     }
    22.  
    23.     public void PressureReliefToggle()
    24.     {
    25.         if (PressureRelief == true)
    26.         {
    27.             PressureRelief = false;
    28.         }
    29.         else {
    30.             PressureRelief = true;
    31.         }
    32.     }
    Note: The first one ( public void PowerToggle() ) works. The others do not. All of the variables are public.

    Edit: Now I am extremely confused because PressureReliefToggle() just started working but CoolantToggle() still will not function. I did not change anything and this is the first time it has worked.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Have you Debug.Log-ed the values to prove to yourself they are not changing?

    Mind you, you can just flip a boolean with
    someBool = !someBool
    .
     
  3. Norteous3

    Norteous3

    Joined:
    Jan 2, 2023
    Posts:
    16
    I have tried to use that to flip the boolean but for some reason that also did not work. I have added Debug.Log to the end of it and the inspector doesn't show the boolean change, but the Debug.Log is triggered.

    This is how I had the Debug.Log set up:

    Code (CSharp):
    1. public void PressureReliefToggle()
    2.     {
    3.         if (PressureRelief == true)
    4.         {
    5.             PressureRelief = false;
    6.             Debug.Log("PressureReliefFalse");
    7.         }
    8.         else {
    9.             PressureRelief = true;
    10.             Debug.Log("PressureReliefTrue");
    11.         }
    12.     }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Debug the value, not some string, so you can see what the value actually is.
    Code (CSharp):
    1. Debug.Log("Pressure Relief: " + PressureRelief, this);
    This will also highlight the game object when you click on it, so you can see what game object is firing it.

    Might be a case of referencing the wrong component, or too many components, or referencing a prefab and not an instance by mistake.
     
  5. Norteous3

    Norteous3

    Joined:
    Jan 2, 2023
    Posts:
    16
    Thank you, the values were changing, I guess something is wrong in a different part of the code.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    What you're avoiding is not the reason why this doesn't work.

    Consider that the
    if
    clause expects the expression to evaluate as a Boolean value.
    It makes very little sense to write
    Power == true
    because Power already is either true or false.

    Instead of writing
    Code (csharp):
    1. public void PowerToggle()
    2. {
    3.   if (Power == true)
    4.   {
    5.     Power = false;
    6.   }
    7.   else {
    8.     Power = true;
    9.   }
    10. }
    You can achieve the same if you would write
    Code (csharp):
    1. public void PowerToggle()
    2. {
    3.   if (Power)
    4.   {
    5.     Power = false;
    6.   }
    7.   else {
    8.     Power = true;
    9.   }
    10. }
    Now observe that all you're doing is taking the existing value and flipping it around, and suddenly you don't need
    if
    at all.

    Code (csharp):
    1. public void PowerToggle()
    2. {
    3.   Power = !Power;
    4. }
    Why confuse yourself with so much code that doesn't do anything special?
     
  7. Norteous3

    Norteous3

    Joined:
    Jan 2, 2023
    Posts:
    16
    I was confused and for some reason the code was not working when I used !Power. It was likely some other bug that I still need to fix.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    The problem has to be somewhere else. You can judge just by looking at how simple this code is. What takes 32 lines of code in your example, would take only 3 in mine.
    Code (csharp):
    1. public void PowerToggle() { Power = !Power; }
    2. public void CoolantToggle() { CoolantOn = !CoolantOn; }
    3. public void PressureReliefToggle() { PressureRelief = !PressureRelief; }
     
  9. Norteous3

    Norteous3

    Joined:
    Jan 2, 2023
    Posts:
    16
    Thank you, I just fixed the unnecessarily long code and replaced it with the shortened version. I am currently trying to find the other bugs in my code that are likely in the Update() section, as that is what the variables I am changing affect.
     
    orionsyndrome likes this.