Cannot covert UI.Button to GameObject via a built in Conversion

Hello Everyone!

I’m having an issue when I try and instantiate a new button at runtime. The relevant code is:

public Button button;

void Start()
     GameObject go = Instantiate(button) as GameObject;

Yields the error:

Assets/Scripts/DraftingScript.cs(122,61): error CS0039: Cannot convert type UnityEngine.UI.Button' to UnityEngine.GameObject’ via a built-in conversion

I couldn’t find anything relevant to buttons out there. Any help is appreciated!


You trying to cast a Button object to a GameObject.
Try this

GameObject go = ( Instantiate(button) as Button ).gameObject;

Or change public Button button to public GameObject button;