Assigning gameobject button to button in script

I have create a GameObject called MyButton and have been able to assign a method in my script to it, so that when I click the button the method is succesfully called.

But I what I can’t figure out, is how to actually turn the button on/off.

So the GameObject itself is called MyButton (with a child called Text for the text that appears on the button).

In my script at the top I have declared the button with:

public Button MyButton;

The method that is called when the button is clicked looks like this, and I am trying to turn it off when I click it:

public void clickMyButton() {  
  Debug.Log ("button clicked" + "\n");
  MyButton.gameObject.SetActive (false);
}

The line where I am trying to SetActive(false) is giving me this error:
NullReferenceException: Object reference not set to an instance of an object

So the button recognizes the script when clicking on it, but the script does not seem to recognize the button.

Am I missing something here that would allow the script to turn the button on/off?

Thanks

That public variable will create a slot in the Inspector. That slot will be empty until you put something in it; drag and drop your button object.

Ahh okay, I see now.

Thanks

Okay this is interesting… when I try to drag the object over onto the slot, it won’t let me.

When I click on the Select button beside the slot, the only option I have to choose is an Assets tab with NONE in it.

That variable is defined with the Button type, so you can only drag on an object of that type. Are you sure you created one of those objects, and not something else that sounds similar?

Thanks for response again…

Okay so I was trying to drag the button object into the slot of the script that was in the project directory. I created a new empty gameobject and then added the script as a component and then I can now drag the button into the slot of this script.