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 Polaris007 · May 15, 2014 at 06:21 PM · loopfor-loopmatrixspacing

How to put spacing between Gameobjs in loop

m generating a (4X4)matrix of cubes in the start function and wish to put spacing between columns does anyone know the right way to do it

void Start () { for (int i=0; i

              for(int j=0;j<GridHeight;j++)//column
              {
                  spacing =new Vector3(2,0,0);
                  GameObject g=new GameObject ();
                  g.transform.position+=spacing;
                  g=Instantiate(gemPrefab, new Vector3(i ,j,0),Quaternion.identity)as

GameObject ;

                  g.transform.parent =gameObject.transform;
                  gems.Add(g.GetComponent<Gem>());
              }
          }
          gameObject.transform.position = new Vector3(-2.5f,-2f,0f); 
      }
Comment
Add comment
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

3 Replies

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

Answer by Polaris007 · May 16, 2014 at 01:11 AM

 void Start ()
     {
         int temp;
 
         for (int i=0; i<GridWidth; i++) //row
         {
 
             for(int j=0;j<GridHeight;j++)//column
             {
                g=Instantiate(gemPrefab, new Vector3 (i,j,0),Quaternion.identity)as GameObject ;
                 g.transform.parent =gameObject.transform;
                 temp=j;
                 g.transform.position+=new Vector3(i+1,temp+1,0); //THIS LINE
 
                 gems.Add(g.GetComponent<Gem>());
 
                }
 
         
         }
         gameObject.transform.position = new Vector3(-2.5f,-2f,0f); 
     }


after tinkering a bit i found a way to do it..the commented line

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 Jeff-Kesselman · May 15, 2014 at 06:27 PM

I think you are over complicating this.

Try this{

 void Start () { 
     Vector3 position = new Vector3(0,0,0);
     
     for (int i=0; i<GridWidth; i++) //row {
          for(int j=0;j<GridHeight;j++)//column
          {
               GameObject g = Instantiate(gemPrefab,position,Quaternion.Identity);
               position.X += <xSpacing>;
          }
          position.Y += <ySpacing>;
      }
 }

xSpacing should be the xWidth of the object plus the empty space you want in between. ySpacing should be the yWidth of the object plus the empty space you want in between.

Comment
Add comment · Show 1 · 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 Polaris007 · May 16, 2014 at 01:28 AM 0
Share

i found this a bit confusing but managed to find a solution g.transform.position+=new Vector3(i+1,temp+1,0); just had to put this

avatar image
0

Answer by Oniony · May 15, 2014 at 06:28 PM

Why not make an int that adds every time the loop goes through? Example: on the first loop it is 0. The gets 2 added to it. So second loop it is 2 then gets 2 added to it. etc.

Comment
Add comment · Show 1 · 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 Polaris007 · May 16, 2014 at 01:26 AM 0
Share

thank u for the hint

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

22 People are following this question.

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

Related Questions

How to stop a for loop before it completes it's full cycle? 1 Answer

"For" Loop not working second time 1 Answer

Even more loop problems 2 Answers

Finding nearest object with a certain tag without for loops. 1 Answer

How to deactivate all GameObject in a array, except last one 4 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