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. Dismiss Notice

Resolved Boolean won't let me change it when the game is running.

Discussion in 'Scripting' started by esrees2, Apr 19, 2022.

  1. esrees2

    esrees2

    Joined:
    Mar 31, 2022
    Posts:
    5
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (shouldFadeToBlack)
    4.         {
    5.             fadeScreen.color = new Color(fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, Mathf.MoveTowards(fadeScreen.color.a, 1f, fadeSpeed * Time.deltaTime));
    6.             if (fadeScreen.color.a == 1f)
    7.             {
    8.                 shouldFadeToBlack = false;
    9.             }
    10.         }
    11.     }
    This code is meant to fade the screen to black whenever the boolean shouldFadeToBlack is true, and yet for some reason unity won't even let me make the value true in the editor.

    It's hard to demonstrate, but in the video below whenever the game is running it won't let me change the value of shouldFadeToBlack, but when the game isn't running I can change it just fine.



    does anybody have an idea what the problem is?
     
  2. nijnstein

    nijnstein

    Joined:
    Feb 6, 2021
    Posts:
    78
    are you sure fadeScreen.color.a is set to 0 when the boolean is set to true? it looks like the alpha channel is never reset
     
    esrees2 likes this.
  3. esrees2

    esrees2

    Joined:
    Mar 31, 2022
    Posts:
    5
    Figured it out. The alpha value was already 1 so it kept turning the value false, i just couldn't see that because I deactivated the image
     
  4. esrees2

    esrees2

    Joined:
    Mar 31, 2022
    Posts:
    5
    yeah that was the problem lol, i just had the image deactivated so I didn't notice
     
  5. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    Yes: when the game is running, your code is setting shouldFadeToBlack to false as soon as you're clicking it true.

    I don't know why exactly, but that's what's happening. I use bools to trigger things from the inspector sometimes, resetting them to false in code, and that's the behaviour I see: I never get to see the tick.