-2
\$\begingroup\$

What should be the C# syntax for the condition if (This Button is interactable)?
I have a script attached to the button, Which has to run only if the button is interactable.

Thank you in advance!

\$\endgroup\$
1
  • \$\begingroup\$ Downvote: this is something you can probably find within minutes of research, I personally try everything I can before posting a question. \$\endgroup\$
    – Pow
    Commented Feb 29 at 0:47

2 Answers 2

3
\$\begingroup\$

Once you have reference to a Button, you can use Button.interactable to determine if the button is interactable. interactable is a member of the UI.Selectable class; Button inherits from Selectable, and thus, has access to interactable.

The below script takes a Button reference, and runs logic if the button is selectable. When you intend to "run the script", simply call Run(). The logic will not run, if the Button is not interactable.

public Button button;

public void Awake()
{
    if(button == null)
    {
        button = GetComponent<Button>();
    }
}

public void Run()
{
    if(button != null && button.interactable)
    {
        // run your logic, here.
    }
}
\$\endgroup\$
0
1
\$\begingroup\$

Checking button.interactable checks only if the button itself is interactable, completely ignoring the possible existence of Canvas Groups.

The correct check should use IsInteractable() instead of interactable:

public Button button;

public void Awake()
{
    if(button == null)
    {
        button = GetComponent<Button>();
    }
}

public void Run()
{
    if (button != null && button.IsInteractable())
    {
        // run your logic here.
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ This seems wrong. Your first part of the condition does not make much sense. And while IsInteractable() is a nicer way to access the bool, it still seems to go as well to check against interactable \$\endgroup\$
    – Zibelas
    Commented Feb 28 at 16:45
  • \$\begingroup\$ According to the code, it also checks m_GroupsAllowInteraction \$\endgroup\$
    – DMGregory
    Commented Feb 28 at 18:25
  • \$\begingroup\$ @Zibelas it is not a nicer way of acessing the bool. That is the point. This function and the property test for totally different things (which is omitted in Unity documentation). The bool tests if this selectable's flag is set to true while the method checks if the selectable overall is interactable (taking into account parents that may have Canvas Group with disabled interaction). \$\endgroup\$
    – Parchandri
    Commented Feb 28 at 20:22
  • \$\begingroup\$ But than that should be explained in your answer why it should check for a canvas group (and not just as a comment) \$\endgroup\$
    – Zibelas
    Commented Feb 28 at 20:59
  • \$\begingroup\$ @Zibelas It does say in his question "Checking button.interactable checks only if the button itself is interactable, completely ignoring the possible existance of Canvas Groups. The correct check should use IsInteractable() instead of interactable" \$\endgroup\$
    – Pow
    Commented Feb 29 at 0:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .