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 :) 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 :)
Thanks in advance. P.S. Sorry for my english :)
I think this could be done with a custom IComparer. The IComparer would acess the boudns of two elements and check which one is bigger.
Thank you for reply. But i don't need to compare them. I need to instantiate them one by one given the size of each prefab, it doesn't matter what will be first - smaller or bigger. $$anonymous$$ain question is about "how to make next prefab instantiated right to previous prefab's border". But your information is good too, i didn't know about IComparer, but now I do :)
Answer by FortisVenaliter · May 06, 2016 at 09:46 PM
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.
Nice! I will try it tomorrow :) Right now I have 1.00 A$$anonymous$$ :) Thank you for advice, and for the fast reply!
So, I tried the way you suggested. This is the almost right way, except some small detail (last step in my list) :)
We create variable public float currentY outside "for loop" that stores our Y coordinate
public float currentY;
Then after prefab1 was instantiated we reassign currentY with prefab1.transform.position.y + prefab1.bounds.extents.y
var prefab1 = (GameObject)Instantiate (blockArray [0], new Vector2 (0, -2), Quaternion.identity);
var prefab1Rend = prefab1.GetComponent ();
currentY = prefab1.transform.position.y + prefab1.bounds.extents.y;
After that, we instantiate prefab2 with our updated currentY coordinate:
var prefab2 = (GameObject)Instantiate (blockArray [1], new Vector2 (0, currentY), Quaternion.identity);
And then, after prefab2 was instantiated witn currentY for y-coordinate, we set transform position of it, adding it's bounds.extents.y
var prefab2Rend = prefab2.GetComponent ();
prefab2.transform.position = new Vector2 (0, currentY + prefab2Rend.bounds.extents.y);
Without last step, next prefab doesn't fit previous prefab's border.
That's it. Thank you very much :)
Your answer
Follow this Question
Related Questions
Spawning limited GameObjects at a specific position not working 1 Answer
Trying to spawn enemies on only one path 1 Answer
Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer
Can I spawn with an array in order? 2 Answers
Instantiate a prefab at three specific x positions 0 Answers