Is there a way to Instantiate a button prefab as a child of a Canvas?

I’m trying to instantiate different buttons, depending on the character preset the player chooses in the menu screen.

I have this script attached to the Canvas gameobject in Unity:

[SerializedField] GameObject spawnPoint;
[SerializedField] Button [] abilityButton;

void Start()
  if(StaticVariableContainer.characterSelect == "Small")
    Instantiate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transform.rotation);

Note: the spawnPoint object is just an empty object I placed in the position I wanted the button in so I had a transform to pass to the instantiated object.

Currently, this will spawn an instance of the button, but at the top of the heirarchy, as it’s own independent gameobject with no parent. If I move it under the Canvas after it’s been instantiated it explodes in size, filling up the screen.

Is there a way to Instantiate the button directly as a child of the Canvas? When messing around in Editor, dropping the prefab onto the Canvas object resulted in a functional button being placed right where I wanted it.

I’ve also tried passing in the Canvas’ position and rotation, as well as not passing any transform data at all.

Simply use transform.SetParent(transform):

GameObject myButton = Instantiate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;


I am working on 2D game, I have a issue here how I place button prefabs randomly moving in downward direction?
I just build button prefabs now I want to spawn downward direction.
Can you please help me to resolve the issue?