Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by IgorKo · May 06, 2016 at 06:17 PM · instantiatearrayspawnorder

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · May 06, 2016 at 06:27 PM 0
Share

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.

Array.Sort $$anonymous$$ethod (Array, IComparer)

avatar image IgorKo Cherno · May 06, 2016 at 09:40 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorKo · May 06, 2016 at 09:55 PM 0
Share

Nice! I will try it tomorrow :) Right now I have 1.00 A$$anonymous$$ :) Thank you for advice, and for the fast reply!

avatar image IgorKo · May 11, 2016 at 03:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges