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 Chimera3D · Jul 20, 2012 at 05:02 AM · sizegridproceduralloopstransforms

How can I create a grid of transforms?

I want to procedurally create a grid of transforms as large as I specify, I do know that I will be using a loop however I don't fully understand loops. I just need someone to write some psuedo code and give an explanation and I can go from there.

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 Seth-Bergman · Jul 20, 2012 at 05:32 AM

Edited code: This should be what you want.. you have to initialize prefab to the object you are creating.. Either by simply drag and drop in the inspector, or by scripting if necessary.

This could be done in the Start function:

 function Start(){
 prefab = GameObject.FindWithTag("grid object"); // for example
 }


As for setting the name, probably not necessary, you can access them all via the array, as in:

my2DArray[x,y] (where x and y are the grid coordinates, from 0 to 9 in this case..)

you create a two-dimensional array:

 var x = 10;
 var y = 10;
 var spacing : float = 10;
 var prefab : GameObject;
 
 var my2DArray : GameObject[,] = new GameObject[x,y];
 
 function Start{
 
 for(var i = 0;i < x;i++)
 {
 for(var j = 0;j < y;j++)
 {
 
 my2DArray[i,j] = Instantiate(prefab, Vector3(j * spacing,i * spacing,0), transform.rotation);
 
 j++
 }
 i++
 }
 
 }

by nesting the loops this way, it goes through the whole thing hope this helps

EDIT:

to clarify-

for(var i = 0;i < x;i++)

the first part (var i = 0;) is the var you will use to increment through the loop

the second (i < x;) is the condition which,when no longer met, will end the loop

the third (i++;) is the incrementation of the var

so, we start with i, which is equal to 0. Each pass, we do i++ ( i + 1 ). When i is no longer less than x (which is 10), the loop ends (does not get entered if condition is not met)

once the inner loop completes a full cycle, it ends, causing the outer loop to increment by 1, then re-enter the inner loop.. if that makes sense

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 Chimera3D · Jul 20, 2012 at 05:56 AM 0
Share

the your code here part is where I really need help but thanks for clearing the loops part up, now I just need to know how to instantiate several gameObjects, arrange them in a grid, and name them A1, B1, B2, ect.

avatar image Seth-Bergman · Jul 20, 2012 at 06:09 AM 0
Share

try my updated code above :)

avatar image Hi_No_Te Seth-Bergman · Sep 11, 2016 at 01:10 AM 0
Share

I've been doing the same thing, both with for-loops, foreach-loops, and for-loops with arrays (exactly like your example), but each time it produces the max number of tiles, but it does it in a diagonal, stacking the rows in this diagonal! I'll post my code, but I know I'm years off here...

avatar image
0

Answer by flamy · Jul 20, 2012 at 06:21 AM

 // im assuming that you need to generate in xz plane...
 
 var ObjectToSpawn : Transform;
 
 function GenerateTransforms(xLength:int,zLength:int,startPosition:Vector3)
 {
 
   for(var i=0;i<xLength;i++)           // switches to the next column of z, continues until xLength times it has switched.
   {
     for(var j=0;j<zLength;j++)         // generates zLength number of transforms along z axis.
     {
       var temp:GameObject = (GameObject) Instantiate(ObjectToSpawn,
                        Vector3(startPosition.x+ObjectToSpawn.localScale.x*i/*+offset if any*/,           
                                startPosition.y,
                                startPosition.z+ObjectToSpawn.localScale.y*j/*+offset if any*/),
                        ObjectToSpawnRotation);

          //places the object according the start position.
       
     }
 
   }
 
 }
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 elesser · Jul 21, 2012 at 07:35 AM

what we are trying to work out with the grid is an AI system, the idea is to set up a grid so that when the player clicks a point to move a unit to the computer can move it in a non stupid way. like say the unit is in D5 and the player clicks in A3 the unit would move and when it hit a trigger to change directions it looks at 2 arrays to know what way to go. in other words if it needed to turn left or right the arrays would say have letters and say left had A B C and right had E F G it would check see A in the 1st one and know to go left same for up and down 1 2 3 4 and the 2nd was 6 7 8 9 then it would check see 3 in the 1st one and know to go up. maybe not the best way to do something like this but its the idea i had

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

8 People are following this question.

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

Related Questions

How to fill a volume with a procedurally generated 3d voxel grid? 0 Answers

How to generate a grid for procedurally generated platforms 1 Answer

Increase height of panel w/ gridlayout when new objects added 1 Answer

Problem with arrays and while loops 0 Answers

GridLayout dynamic cell size based on object count 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