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!

  • Workaround for that, i always make sure that the target image has "Raycast Target" set to false. – Omar Guendeli Dec 5 at 14:14
  • 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. – Prometheus Dec 5 at 16:55
//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;
     }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.