Clone a button

Hi all,

I have six GameObject that i am spawning. I use the clone method for it.
Now at a certain point, the GameObject need to display a countdown button. Every cloned GameObject, needs his own button.

I have a countdown button in the scene.
I use the following to let the button follow the GameObject, and it works.

Vector3 btnPos = Camera.main.WorldToScreenPoint(this.transform.position);
btn.transform.position = btnPos;

The problem is, the one button in the scene, hops over to the clone objects.
So, i tried to clone the button the same way i clone the other GameObjects and i try to assign them.
But somehow it doesn't show anything.

Can somebody help me with this.

Wen you clone it you must assign it it the parent object.

 GameObject _btn = Instantiate(_btnPrefab);
_btn.transform.SetParent(_theParent.transform, false);

Exactly what @gaglabs said but you can also do it in a single step to reduce the chance of it being missed, and also to make sure you don't omit the critical second optional boolean argument to .SetParent():

GameObject _btn = Instantiate<GameObject>(_btnPrefab, _theParent.transform);
1 Like

guys I'm trying to clone a button and then move it and clone it again and so on...
so i can make a grid that i generate through code but how do i clone the button each time i move it and keep the position of witch the button i cloned it from before.