Make a button disable its self. (c#)

I have a button set to switch some active models using the .SetActive script.
However I also want to to disable its self and replace with another button. I just cant figure out how to name the buttons. if it was something like “if (GUI.Button(button1) (new rect…” i could simply disable button1 but I have 2 buttons so far with plans to have about 8-10 and I need to disable this exact button without affecting the others.

Is there a way to reference individual buttons without deleting my entire UI ?

(I am using unity 5)
I currently have a system of toggles which activate and deactivate depending on certain conditions, I consider the code very messy but it does the trick. In this kind of script I prefer to hard code the entire logic system. For example, I would include

using UnityEngine.UI;

public Button button1
// and the other 8-9 buttons, this list gets long depending on how many
// objects you wish to manipulate

once you have all your buttons listed, you can go into the editor and drag and drop each one into their respected spot. If you wish to still see the button but not be able to use it based on conditions, you can create a for loop function (and put that function into update) and set the button as such (this is a generic example I thought of)

if (!playerHasLevel)
button1.interactable = false;
else if (playerHasLevel)
button1.interactable = true;

and if you don’t want the user to see the button I would strongly recommend disabling the Image and Interactable components vs deactivating the object, as I am currently having issues with activating a music manager again after scene loads (I’m going to be switching to the first part after I’m done tweaking some other things so I’m not exactly sure how to do that yet but I imagine it’s not too hard)

I hope this helps you out!

Don’t know if you managed to figure it out untill today (this post is from 2015) but here it is:


Hope it helps other people with the same doubt, cause I had this same doubt.

Since this question got already bumped I’ll post an answer for IMGUI buttons.

Generally the immediate mode GUI is stateless. So if you want an enabled / disabled state for your button you need to use a variable to hold that state. An GUI element is actually displayed enabled or disabled based on the GUI.enabled setting. So all you have to do is set GUI.enabled right before you draw / handle your button. Make sure you set enabled back to true after your button otherwise everything that follows would be disabled as well.

bool state = true;

void OnGUI()
    GUI.enabled = state;
    if(GUI.Button(new Rect(10,10,60,20), "Disable Me"))
        state = false;
    GUI.enabled = true;
    // some other controls

Also, depending what your purpose is, this may help too…


Index is the position your button is located as a child (example: a gameobject with 4 children, and your button is the 4th child, it index will be 3)