Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Bytebait · Dec 31, 2011 at 09:36 AM · instantiatefor loop

Instantiate for loop problem

On an empty game object I have a script that is trying to instantiate a creation of a large block created from several smaller blocks.

I'm trying to instantiate a block of 10x10x10 of cubes 2m apart.

((This is only for my understanding of how to do this correctly. I could just make a prefab and call that once, but I'm curious as to how this could be solved.))

Here's my bad attempt at solving this problem.

 var prefab : Transform;



for (var i : int = 0;i < 10; i++) {

Instantiate (prefab, Vector3(i * 2, 0, 0), Quaternion.identity);

Instantiate (prefab, Vector3(0, i * 2 + 1, 0), Quaternion.identity);

Instantiate (prefab, Vector3(0, 0, i * 2 + 1), Quaternion.identity);

}

This only makes a 10x10x10 along the x,y,z lines, however I can see right away that I'm messing up somehow as the first position of the y and z aren't in the correct location.

Comment
Add comment · Show 1
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 ByteSheep · Dec 31, 2011 at 02:25 PM 0
Share

I'm really not great at for loops but I think the reason your y and z prefabs aren't being instantiated at the right position is because you are saying i*2+1 and since i is 0 the first time (I think) you are practically saying 0*2+1 which will equal 1. Since you want it to appear at 0 and not 1 you might have to remove the +1. This might get you started =)

2 Replies

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

Answer by AtomicHippo · Dec 31, 2011 at 03:44 PM

It's probably best to use 3 for loops here, one for each dimension. Something like;

     var prefab : GameObject; 
     var offset : int = 2;//Distance between cube centers
     
     function Start(){
     
         for (var z = 0;z<10*offset;z+=offset){
     
             for (var y = 0;y<10*offset;y+=offset){
         
                 for (var x = 0;x<10*offset;x+=offset){
     
                      Instantiate (prefab, Vector3(x,y,z), Quaternion.identity);
     
                      //Added this line so you can actually see how the cubes are being populated
                      yield WaitForEndOfFrame;    
     
                 }    
             }
         }
     }

This builds a row along x first, (for 10) then generates 10 of those rows along y(for 10x10), then duplicates the two of those along z (for 10x10x10).

Think that should work. Happy new year!

Comment
Add comment · 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
0

Answer by Bytebait · Dec 31, 2011 at 06:06 PM

A great reminder of how far I have to go in my programming. Thanks AtomicHippo!

Also, Love the yield WaitForEndOfFrame function!

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

[C#] How do I instantiate multiple objects at the same time? 1 Answer

A way to destroy these blocks faster? 1 Answer

Assign Names Problem 1 Answer

Instantiate only one Prefab per 40 unit 2 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