- Home /
How i instantiate button and name them.
How i instantiate 5 button by using 1 button and keep the name of button differently.can u suggest how it is possible.. hey i asked i have one game object called button, now i trying by using this gameobject i create 5 different game object and add different name using text script on this gameobject . if u further explain let me know.
you change the prefab then you instance.
for (int x = 0; x < 5; x++)
{
button.name = "button" + x.tostring;
instantiate(button);
}
there is now button 1, button 2, button 3, button 4 and button 5.
$$anonymous$$ark as answered please. :)
thanks for ur help but i need more help i try to store name like play , start,stop, not button1,button2 like that can u suggest
make an array of strings
names[0] = "Play"
names[1] = "Start"
for (int x = 0; x < 2; x++)
{
button.name = names[x];
instantiate(button);
}
that will name the first button play and the second start
should be pretty easy to add more names :)
don't forget to mark as answered :)
hey everything work well , but it not take the name of button.
hmmm? button.transform.name maybe? button.tag should definately work
An instance should copy button, I dont know why it wouldnt work :(
Answer by sparkzbarca · Nov 16, 2012 at 04:45 PM
Well try doing it after the fact. That and maybe it was an issue with not casting it. (something I recently learned about).
//technically i think instantiate makes an object an object //not a game object. Maybe this will fix is.
public UIButton button;
GameObject[] Buttons;
Buttons = new GameObject[3];
for(int x = 0; x < 3; x++)
{
Buttons[x] = instantiate(button) as GameObject;
}
Buttons[0].name = "play";
Buttons[1].name = "pause";
buttons[2].name = "rewind";
You might want to cap the I of Instantiate :) and it will tell you that parameters are missing I would think (position, rotation).