Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 IMTRIGGERHAPPY9 · Sep 07, 2011 at 11:49 PM · instantiatearrayfor-loopyx

adding "X" amount of objects to an array

The code i have right now only goes until 9 on each X and Y array. what i want to know is how would i continue it ( ill explain more after the code)

 var instantiateObject : Transform;
 
 var gridX = 10;
 var gridY = 10;
 
 
 function Start () {
 //create two new arrays for the x and y axis
 tileArrayY = new ArrayList();
 tileArrayX = new ArrayList();
 
 
     for ( var x = 0; x < gridX; x++){
         
         for (var y = 0; y < gridY; y++){
         
             var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
             myName.name = "x: " + x + "y: " + y;
             // assign each y and each x to the individual arrays
             if( y % gridY == 0 ){
             tileArrayY.Add(myName.gameObject);
             }
             
             if( x % gridX == 0 ){
             
             tileArrayX.Add(myName.gameObject);
                 
             
             }    
         }
         
      } 
     // check those arrays
     for( var i = 0; i < 20; i++){
     
         print("This is your YArray: " + tileArrayY[i]);        
         print("This is your XArray: " + tileArrayX[i]);
     }
 }

so what i am getting from this code is:

Xarray:

x:0 y:0

x:1 y:0

x:2 y:0

x:3 y:0

.....

x:8 y:0

x:9 y:0

Yarray:

x:0 y:0

x:0 y:1

x:0 y:2

x:0 y:3

.......

x:0 y:8

x:0 y:9

but i want them to do this instead:

Xarray:

x:0 y:0

.....

x:9 y:0

x:0 y:1

.....

x:9 y:1

i hope this makes sense? ask me to clarify if you dont understand it and ill be happy to help you help me :) thanks

Comment
Add comment · Show 2
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 aldonaletto · Sep 08, 2011 at 01:04 AM 0
Share

This is really confusing. What you have now is two arrays with 10 elements each; is that what you actually want, or should it be a bidimensional 10x10 array, thus containing 100 elements and covering completely a rectangular area (from the name tile, that's what I suspect you actually need).

avatar image IMTRIGGERHAPPY9 · Sep 08, 2011 at 01:06 AM 0
Share

yes that is what i wanted an multidimensional array, but i couldn't figure out how to put the x's in one and the y's in another. so i decided to go with two seperate arrays ins$$anonymous$$d, but i do need it to be 10x10. so i am restricting it some how?

2 Replies

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

Answer by SilverTabby · Sep 08, 2011 at 02:53 AM

I would stay away from the Class-based Arrays in Unity (and in general) - the classes like "Array" and "ArrayList". I have had nothing but trouble with them.

Here is the basic syntax for working with built-in multi-dimensional arrays in JavaScript, which I have never had any problems with:

 var myArray : int[,] = new int[5,5];
 myArray[0,1] = 42;
 for(var X : int = 0; X < myArray.GetLength(0); X++)
     for(var Y: int = 0; Y < myArray.GetLength(1); Y++)
     {
         myArray[X,Y] = X+Y;
         Debug.Log(myArray[X,Y]);
     }

 var threeDimensionalArrayOfObjects : GameObject[,,] = new GameObject[9,6,42];
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 IMTRIGGERHAPPY9 · Sep 08, 2011 at 03:11 AM 0
Share

yeah somebody was telling me this earlier, but i couldn't figure out how to implement it into what i was doing. and all around i dont understand it ha ha. like the part where you say myArray.Length(0); i dont understand what that code is doing at all.

avatar image SilverTabby · Sep 08, 2011 at 09:32 AM 0
Share

This code is just the basics you need to create and use 2-d arrays.

If you understand 1-d arrays, this code is all you need to add on the second dimension.

the .GetLength(N) is how the .NET library returns the length of the Nth dimension of a multi-dimensional array (because .length doesn't return enough information)

avatar image IMTRIGGERHAPPY9 · Sep 11, 2011 at 06:35 PM 0
Share

this really did help alot lol my thinking was completely wrong on this, but ive figured it out now thanks!

avatar image
0

Answer by simonmc · Sep 08, 2011 at 02:51 AM

Changing the loop to:

 for ( var y = 0; x < gridY; x++){
 
    for (var x = 0; y < gridX; y++){
 
      var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
      myName.name = "x: " + x + "y: " + y;
        
      tileArrayX.Add(myName.gameObject);
 
    
    }
 
 } 


Will give you one array, tileArrayX of the form you said. Note that the y loop has been moved to the outside.

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 IMTRIGGERHAPPY9 · Sep 08, 2011 at 03:13 AM 0
Share

this will add all of the game objects to one array though. the problem i have with this is its a lot harder to define the x and y values of each, which for what i am doing i really need.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object with object set, 1 Answer

Instantiate object in for loop with array 2 Answers

Can't assign an instantiated class to an array? 1 Answer

each instantiation getting it's own draw call? 1 Answer

A null value was found where an object instance was required. 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