0
\$\begingroup\$

my button can still be pressed in-game even if I set it to button.interactable = false;

What I'm trying to achieve is when the Panel CropsPanel is opened, or active in Unity, I want the EquipmentButton to be disabled and to be not usable by the user. Here is the code I've written:

 public void Start () 
     {
         GetComponent<Button>().interactable = false;  
         if (CropsPanel.activeInHierarchy == true)
         {
           EquipmentButton.interactable = false;
         }
     }

I have also set all buttons in the inspector, placed a Debug.Log("msg here"); to see if the particular code is working. But I can't seem to get it working. Am I missing something? Thanks guys!

\$\endgroup\$
2
  • \$\begingroup\$ Workaround for that, i always make sure that the target image has "Raycast Target" set to false. \$\endgroup\$ Commented Dec 5, 2018 at 14:14
  • \$\begingroup\$ I dont think activeinhierarchy works with start. I think there needs to be an listener. Try to quickly add an input in thenupdate function and try to trigger the above function in a new void. Other than that, i also believe that activehierarchy needs a apecific parent. If its still not working, post your declaration and the rest of your script that might be important. \$\endgroup\$
    – Prometheus
    Commented Dec 5, 2018 at 16:55

1 Answer 1

1
\$\begingroup\$
//using public you can directly drag a button from Hierarchy and drop it in inspector
public Button EquipmentButton;
void Start()
{
//You can link the button on runtime using this line.
  EquipmentButton = GameObject.Find("Button_Name_In_Hierarchy").GetComponent<Button>();
//Now disable it
  EquipmentButton.intractable = false;
}

initially CropsPanel is disable means don't declare it in start function. keep it in an update or create some other function to check it

void Update()
{
  if (CropsPanel.activeInHierarchy == true)
     {
       EquipmentButton.interactable = false;
     }
}
\$\endgroup\$

You must log in to answer this question.

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