Precisely specifying the position of an instantiated object... (Solution: Dont do it with coding)

Hello :wink:

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:

  • Empty
  • Button
  • Name
  • Name2

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 :wink:

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 :slight_smile:
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.