Make a button not clickable

Hi everyone, I recently switched to unity 2020 and some things seems to have changed, becoming more difficult. After nearly 2 hours of googling “how to make a button not clickable” I have given up, also because I feel like it shouldn’t be this hard.

Can you guys tell me how to make a button not clickable from code in c# ?
What I have is:

GameObject.Find("btn").GetComponent<Button>().??????

Thank you for the help, I will try to document myself more. If you guys have useful sources, I would appreciated that.

Does this work?

GameObject.Find("btn").GetComponent<Button>().enabled = false;

Unfortunately that property does not exist for the object button.

Following what you said, I have tried also :

GameObject.Find("btn").GetComponent<Button>().SetEnabled(false)

But the button is still clickable.

It showed up for me. That is very strange, but I am using the latest beta version so maybe that is the issue!

Can you check this and see if it works in the editor?

7428257--909518--Screen Shot 2021-08-18 at 9.40.56 AM.png

Thank you for helping haha. Which Unity version are you using ?? I find it also very weird !

1 Like

2021.2.0b7

and np! :slight_smile:

I installed the 2021.1.17f1 and the “.enable” does not seem to be there. I don’t want to upgrade to a beta version for now so I need to find another way to deactivate the button. :frowning:

Oh, I thought you disabled the interaction in the editor, my bad! You could also try this?

canvas.GetComponent<Button>().interactable = false;
1 Like

“.interactable” seems to be available for InputField but not for Buttons, it’s super weird. That’s why I created this post here.
Although is not the best way, at the moment I change position of the Button.

1 Like

The problem was that my code editor kept putting the Button class with a different import.

using UnityEngine;
using UnityEngine.UI;

These are the import that do not mess up the class button.

1 Like