Spawn prefabs from array in specific order given the size of prefab

Hi everyone!

This is not another question like “help me to spawn from array” when there are a lot of code examples for spawning :slight_smile:
My question is about spawning prefabs in specific order, given size and position of ther prefab’s.
I read a lot of existing topics before I decide to ask you guys, but found nothing to this question.

So, the question is:
We have array of prefabs. I want to instantiate this prefabs in specific order, for example - one by one, given the size of previously instantiated prefab.

Like this:

One thought I have - is to use GetComponent (Renderer) () and it’s property such as bounds.size, but I can’t get how to correctly track the size and position of previously instantiated prefab, when we use for loop to instantiate prefabs.

  • Tracking Vector2 position of previous prefab is clear to me (or not at all)))). Maybe I should create temporary variable such as public static Vector2 tempPos, and then rewrite it for each step of loop, and then use it as Vector2 for instantiate position of next prefab.

  • But i can’t get how to use bounds.size of previous prefab to set position of new prefab.

That’s it. I want you guys, to show me the right way (not to write code instead of me) to solve this puzzle :slight_smile:

Thanks in advance.
P.S. Sorry for my english :slight_smile:

If you’re planning on instantiating them in a column like your image, then you would just have a float variable to store the current y-coordinate outside of your loop. Then, you instantiate the objects at the position defined by that coordinate. After you instantiate each object, you add the bound size y to your coordinate y variable. This will offset it for the next iteration of the loop.