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 Ghachii · Apr 12, 2012 at 11:14 AM · javascriptarrayspacespacing

How to shorten the distance between objects in an array?

I found the following code for making an array of cubes:

 var cube : GameObject;
 var x = 100;
 var z = 100;
 
 function Start () {
     for (i = 0; i < x; i++) {
        for (j = 0; j < z; j++) {
           Instantiate(cube, Vector3(i, 0, j), Quaternion.identity);
        }
     }
 }
 
 function Update () {
 
 }


I've tried it, and it works, but the cubes are reaaaaally far apart. Could someone please tell me how to modify this code in order to instantiate the cubes with less space between them. I realise I could just increase the size of the prefab, but I'd like to learn how to do it this way.

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Apr 12, 2012 at 11:20 AM

You can control the distance - and even the position - in the world this way:

var cube : GameObject; var x = 100; var z = 100; var distX: float = 0.5; // set the distance in X var distZ: float = 0.5; // set the distance in Z var pos0: Vector3 = Vector(0, 0, 0); // lower left cube position

function Start () { for (i = 0; i < x; i++) { for (j = 0; j < z; j++) { var pos = pos0 + Vector3(i*distX, 0, j*distZ); Instantiate(cube, pos, Quaternion.identity); } } } HOW TO EDIT A PREFAB: drag it to the scene, modify whatever you want and click the Apply button in the Inspector to update the prefab - you can then delete the scene object, if you don't need it anymore.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with Array script 1 Answer

Javascript How to Activate/Deactivate GameObject Arrays 1 Answer

JavaScript issue: apparently invisible variables 1 Answer

(31,1): BCE0044: expecting }, found ''. 2 Answers

Turn all objects in the array to make same thing 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