Hello 
I want to clone an Object using Instantiate() and a
Assign parent object:
var genericButtonName = Instantiate(structure_prefab, new Vector3(300, -300, 0), Quaternion.identity, mainParent);
genericButtonName.gameObject.SetActive(true);
300 and -300 are just for testing.
This works so far, but unfortunately the positioning is strange.
I found two solutions online, but they all involve setting the position to exactly 0. That would be enough for testing, but since I need a lot of these clones later, they would overlap.
At the moment all positions are set to 0, both from the parent and from the clone. The position in the game is then -630.46(x), -1106.813(y) and 0(z).
Since I later want the buttons to automatically position themselves in such a way that they and the structure that follows them (family tree-like) do not overlap, I cannot determine the position in advance, but I have to leave the possibility of determining the position open.
Does anyone have an idea how I could solve this?
So that I can set the position exactly and it will then appear there? In the example this would be 300, -300, 0.
I will care about the automatic positioning later.
Whatâs the use case here?
I would not dare to change the position of UGUI elements at runtime, let alone instantiate them. Their position is controlled by layout and canvas scaler after all.
Perhaps you should rather instantiate a self-contained âbutton on a canvasâ so that by setting the game objectâs position you also move its canvas. That will likely work better.
You may also want to look into UI Toolkit which makes working with complex GUI a ton easier.
1 Like
At the moment I have the following structure that is used as a prefab:
If I understand you correctly, you advise me to put my own canvas at the top of the prefab?
So add or change Empty into a canvas? Then remove the example position (300, -300, 0) completely?
Iâll try it.
Iâll also take a look at the UI Toolkit, but it will take some time. By the way, thanks for the quick answer 
Edit:
I want to build a structure that looks like a family tree.
Each card in the game is displayed by a button (prefab clone), which displays some information. When you press the button it should âopenâ the card and show all directly subordinate cards below it. I would like it to be generic, that is
no matter which card (i.e. which button) I press, the same function should be called.
This function uses the card ID in the database to find which cards are subordinate to it and displays them, etc.
The only difference is which data from the database of the card (i.e. the clone) is given.
I have now removed the position data and Quaternion.identity and now the clone of the prefab is displayed correctly at 0, 0, 0.
Of course, this isnât a real solution since the position is now always 0, 0, 0. The canvas alone doesnât solve the problem either.
I also donât yet know exactly how to pass the card ID to the button or canvas so that the assignment to the card can be made. I could put the ID in the name, but I feel like it wouldnât be an efficient solution to put it in there and cut it out again when necessary.
Maybe the UI Toolset will help me, especially with positioning, but I have
to take a closer look than I have so far.
Donât do shenanigans like this: you do not want to have to maintain all that noise!.
Instead justâŚ
- make a prototype for each of the âthingsâ you contemplate in your UI
- Instantiate() as many of them as you need
- make sure they are parented into some type of layout controller so you donât have to think about positioning them in code.
As CodeSmile hints, positioning UI stuff in code is a fools errand. Just⌠donât.
See attached for an example of dynamic UI generation and filling out of a small âthingâ that has an icon and a title and accepts button presses.
9557998â1351300âDynamicUIDemo.unitypackage (94 KB)
1 Like
Thanks for your answer Kurt-Dekker 
By prototype you mean the same thing as I mean by prefab?
And with you mean the clones of it?
A system that takes care of the positioning itself sounds good.
Iâll take a closer look at your file tomorrow morning, as well as the UI Toolkit.
Edit:
I think it is also possible to set the maximum number of cards and create the clones accordingly.
The clones that are not needed can then remain deactivated until they are needed.