Search Unity

Button cant detect bool

Discussion in 'Scripting' started by epochplus5, Sep 25, 2021.

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    hi guys

    i have a UI button thats connecting to a script on a prefab. I want the button to work when a bool is true.
    But the method the button is connecting to seems to just ignore the bool.

    This is the method the button is connecting to.
    the bool canDetShell is just ignored though. What can i do to get this working?

    Code (CSharp):
    1. private bool canDetShell = true;
    2.  
    3. public void DetonateShell()
    4.     {
    5.         if (canDetShell)
    6.         {
    7.             print("Detonate shell");
    8.         }
    9.      
    10.     }
     
  2. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hi, you just need to say what the bool should look like (true, false).
    write:
    Code (CSharp):
    1. private bool canDetShell = true;
    2. public void DetonateShell()
    3.     {
    4.         if (canDetShell = true)
    5.         {
    6.             print("Detonate shell");
    7.         }
    8.    
    9.     }
     
  3. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Sorry,
    write: canDetShell == true
    IMPORTANT
     
    Kurt-Dekker likes this.
  4. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Hi, thanks for answering

    if (someBool) is the same as (someBool == true) its just a short way of declaring something true.

    but i worked out the problem

    the bool has to be public and then the check box in the inspector needs to be unchecked.
     
  5. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Sound's good. :)
    I know, but i had the same problem weeks ago.
     
    epochplus5 likes this.
  6. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    will give it a go one sec