Disable button after clicking it

Hi,

I’m trying to create a function that reads a value contained in a button. If the value matches a value in a list, the button is supposed to disappear. It runs whenever the button is clicked (in the OnClick() field in the inspector). Unfortunately, I can’t figure out how to have the script reference the button and have it disable itself.

This is my code:

public void CheckIfARequiredItem()
    {
        string slotValue = gameObject.GetComponent<SlotValue>().value;

        //see if the slot value is a required button
        for (int i = 0; i < requiredItems.Length; i++)
        {
            if (slotValue == requiredItems[i].ToString())
            {
                //add the item to what has been collected
                //and disable the button
                itemsCollected.Add(requiredItems[i].ToString());
                gameObject.SetActive(false);
            }
        }
    }

Your help would be greatly appreciated!

Do you have a reference to the button somewhere in code where you can access it? Like in some sort of UI manager type script that has all of your UI elements in it as variables?

I don’t have a specific script that has all of my ui elements in a variable, but i do have that button and two others referenced in an array in the same script my function is located. I’m not sure how I can have the function know what button is being pressed though since it’s meant to be used when the button is clicked.

It’s not the most elegant thing in the world, but you could always add a second OnClick event on the button itself.

You can pass a single parameter though the button, have the button pass which number it is in your array as it’s argument to a method that disables the button based on that array.

Okay, I will try that.

Thank you for your help.