- Home /
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.
Answer by _joe_ · Mar 31, 2015 at 11:12 AM
Simply use transform.SetParent(transform):
GameObject myButton = Instantiate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
myButton.transform.SetParent(yourCanvasVariable.transform);
I tried a similar method, something like:
Instatniate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transfrom.rotation);
abilityButton[0].transform.parent = canvas.transform;
and it threw an error. Something about not being able to change the parent of a prefab to avoid data corruption.
.parent is deprecated, that's why I used .SetParent(transform) in my example.
@$$anonymous$$ichael_$$anonymous$$iller As signalled by the error message, with that code you're changing the parent of the prefab, not the clone. Whether using .parent or SetParent() you need to do what Joe does and get a reference to the clone then set its parent rather than the prefab's.
Exactly, I was on my phone i wasn't able to clearly read the code.
Simply store the Instantiated object in a GameObject (don't forget the "as GameObject" at the end to convert the Object into a GameObject) and then change its parent.
Be careful, you wrote Instatniate ins$$anonymous$$d of Instantiate.
Oh! Thank you @Bonfire Boy for clearing it up, and thank you @joe for the answer! I totally missed that you were referencing the specific game object. I appreciate you guys helping me figure this out.
Edit: Grammar
Answer by AssoBaba · Aug 15, 2017 at 05:45 AM
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?
Your answer
Follow this Question
Related Questions
Instantiate New Gun 1 Answer
positioning ui buttons in a scrollview 1 Answer
Pressing space calls the wrong function 1 Answer
How to change Image on button click in a prefab that is instantiated 1 Answer
Instantiate a Prefab as child 0 Answers