• 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 DevonJavaScript · Nov 07, 2011 at 05:08 AM · cubegenerationblockspacing

Generating a grid of cubes with space between.

So I was wondering what would be the best way to generate a grid on the X and Z axis with say 5 X and Z Units between eachother? So how would I go about doing this?

Thank you for any help! -Devon

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Addyarb · Apr 18, 2016 at 01:15 AM

Considering this is the first google answer that relates to this question, I figured it's worth answering properly.

Here is some example code to get a space between your grid items!

The magic happens on this line: newRoom.transform.position = new Vector3(x size.x, z size.x, y * size.x);

What we're doing is multiplying our grid item [Read: Cube]'s position by an incremented value. In other words, we're adding a number to the position that increases each time we add a cube.

size.x (in this case) could be anything that gets incremented during the for loop.

Feel free to paste this into the start function/method of a new script and play around with it. I've added the random generation of colors for the cubes to make things more visual.

 Vector3 size = new Vector3 (5,5,1);    //This is the size of our cube-grid. (5x5x1)    
 
 for (int x = 0; x < size.x; x++)
             {
             for (int y = 0; y < size.y; y++)
                 {
                 for (int z = 0; z < size.z; z++)
                     {
                     GameObject newRoom = GameObject.CreatePrimitive(PrimitiveType.Cube);
                     newRoom.name = (x > y) ? "x" + x : "y" + y;
                     newRoom.transform.position = new Vector3(x * size.x, z * size.x, y * size.x);
                     newRoom.GetComponent<Renderer>().material.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
                     newRoom.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                     newRoom.transform.SetParent(transform);
                     }
                 }
             }
         }
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 IMTRIGGERHAPPY9 · Nov 07, 2011 at 05:15 AM

using for statements within each other with an instantiation at the end... you should be able to come up with the rest...

or at least start making your own code instead of asking others to make your game for you

Comment
Add comment · Show 3 · 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 DevonJavaScript · Nov 07, 2011 at 05:18 AM 0
Share

I understand that I'm asking about logic, not code itself.

avatar image IMTRIGGERHAPPY9 · Nov 07, 2011 at 05:22 AM 0
Share

well like i said use two for loops one nested inside another with your instantation placement being the size of your cube for example i have.

Instantiate(instantiateObject, Vector3(x .5,0,y .5), Quaternion.identity);

this is at the end of my for loop and my first loop is using the letter x and the second is using y then x * the size of your cube on the x axis will add a cube exactly one spot away, same going for the y so just put this at the end of your two loops

avatar image IMTRIGGERHAPPY9 · Nov 07, 2011 at 05:59 AM 0
Share

did that help?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Generate objects around object based on bounding size 0 Answers

Problem with floor generator. 2 Answers

Cube made out of many voxels 0 Answers

Can't figure out how to achieve an effect like QUBE 1 Answer

Generate cubes in 5 areas 1 Answer


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