Instantiating prefabs dynamically in script

I have a canvas object. I right click it and add a Button, which defaults to having a rect transform, image script, button script and text child object. I make it a prefab and delete it from my canvas. I want to have a data driven engine so I have an external list which I want to make into buttons. When I have my list loaded I attempt to create dynamic buttons by:

GameObject prefabButton = new GameObject("StandardButton");

This in the hierarchy but it doesn’t have the image or button or text components added, only a transform.

Why doesn’t it have the things the prefab has??

I read I might need to instantiate it, so I tried:

GameObject stdButton = (GameObject)Instantiate(prefabButton);

Again this creates another object in my hierarchy with nothing but a basic transform.

What is the difference between this object and the one I created before? Why instantiate at all?

The first thing you tried will not work, it is only creating a new gameobject with that as its name.

The only easy way would be Instantiating the prefab into the scene. There must be something wrong with how you are getting the prefab, you eathier did not assign it in the editor properlly or is not correct. You can try putting the object into a folder called Resources and using Resources.Load() (Unity - Scripting API: Resources.Load) to get the gameobject and then Instantiate it.

One thing to note, you will HAVE to do is set the parent of the new object as the canvas or object under the canvas to render properly. Positioning is harder but using layout groups can solve that issue.

Edit: If these don’t work, please post scripts involved.