Enable a button when a bull is true

Hello there people :slight_smile: . I have a question. I want to enable a button when a bool is true. I mean I want it to be disabled when a bool is false and enable it when I set it to true.

Your question is a bit vague. You are using Unity UI? Immediate mode UI? NGUI UI? Your own?

I am using unity UI

Button.interactable? See the Unity docs for this property (https://docs.unity3d.com/ScriptReference/UI.Selectable-interactable.html), it includes an example of enabling/disabling a button

(okay, it’s actually Selectable.interactable, but Button is derived from Selectable)

public void ToggleBool(ref bool b){

b = !b; //flip b
myButton.SetActive(b);
}

could also use the interactable property like @T5Shared , but this keep the button visible, SetActive hides it completely

Sure. GameObject.SetActive() to show/hide, Selectable.interactable to enable/disable. He asked for ‘disabled’ and ‘enable’, so I went for that. Did we help you though, @nckbelias ?

all done :slight_smile:
thank you!!

1 Like