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
1
Question by Kroltan · Jan 21, 2012 at 12:07 AM · 2darrayconvertindex

Convert 1D array to 2D array

I am tring to convert a 1D array to a 2D array for better organization, but it won't work. it says the array index is out of range on line 15 of this script:

 class BlockData {
     var heightMap : int[,];
     var floorMap : int[,];
     var wallsMap : int[,];
     var spawnMap : int[,];
     function BlockData(size : int, height : int[], floor : int[], walls : int[], spawn : int[]){
         heightMap = new int[size,size];
         floorMap = new int[size,size];
         wallsMap = new int[size,size];
         spawnMap = new int[size,size];
         var i : int = 0;
         for (var x: int = 0; x < height.Length; x++){
             for (var y: int = 0; y < height.Length; y++){
                 Debug.Log(height.Length+"   "+i);
                 heightMap[x,y] = height[i]; //THIS LINE OVER HERE --- The rest of the code is not even being processed, and it's just copies of this loop chain.
                 i++;
             }
         }
         i = 0;
         for (x = 0; x < floor.length; x++){
             for (y = 0; y < floor.length; y++){
                 floorMap[x,y] = floor[i];
                 i++;
             }
         }
         i = 0;
         for (x = 0; x < walls.length; x++){
             for (y = 0; y < walls.length; y++){
                 wallsMap[x,y] = walls[i];
                 i++;
             }
 
         }i = 0;
         for (x = 0; x < spawn.length; x++){
             for (y = 0; y <= spawn.length; y++){
                 spawnMap[x,y] = spawn[i];
                 i++;
             }
         }
     }
 }
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 jahroy · Jan 21, 2012 at 12:27 AM 0
Share

You might have to loop through and initialize each array:

 for ( var i : int = 0; i < size; i ++ ) {
     height$$anonymous$$ap[i] = new int [size];
 }

I'm not sure... I've never used 2D arrays in Unity, but it's worth a shot.

avatar image Eric5h5 · Jan 21, 2012 at 12:57 AM 1
Share

@jahroy: no, the arrays are already being initialized properly.

2 Replies

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

Answer by aldonaletto · Jan 21, 2012 at 12:59 AM

You're doing the x and y loops height.Length times each, what results in height.Length height.Length* iterations. Since you're incrementing i each iteration, height[i] will go out of range after the first y loop.
If I understood correctly what you're trying to do, your loops should count from 0 to size, not 0 to height.Length, and size should be >= the square root of height.Length:

       ...
       var i : int = 0;
       for (var x: int = 0; x < size; x++){
         for (var y: int = 0; y < size; y++){
          Debug.Log(height.Length+"   "+i);
          heightMap[x,y] = height[i];
          i++;
         }
       }
       ...
As you said, the other loops are copies of this one, thus they all have the same problem.
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 Kroltan · Jan 21, 2012 at 01:16 PM 0
Share

Tactical Facepalm. EDIT: Still doesn't work, it gives the same error. I'm trying to create a 2D arraw with its boundaries at [size,size], to create a square grid.

avatar image
1

Answer by Eric5h5 · Jan 21, 2012 at 12:58 AM

Both the inner and out loops are repeating height.Length times, so the total number of values is height.Length * height.Length, which is clearly way out of range since the total in the array is height.Length only.

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

7 People are following this question.

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

Related Questions

2D array problem in C# 2 Answers

Matching Index of two Arrays after one Array is sort 3 Answers

IndexOutOfRangeException: Array index is out of range 2 Answers

Array error - Index is less than 0... 3 Answers

array problem 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