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 /
  • Help Room /
avatar image
0
Question by Quasar47 · Feb 04, 2018 at 11:17 AM · 2darrayloopindexindexoutofrangeexception

,"For" loop looping only once through my array

Yeah, I know, that error has been reported so many times, and I could fix it by myself so many times, that even I wonder if I am just blind or not.

I have an array[ , ] of Transforms, and two "For" loops, each one looping through one dimension of my array. My array of Tranforms collects all objects inside a grid, and depending on their position, places them at the index [gridWidth, gridHeight]. Nothing too complex so far.

I use this array to establish a correspondance with another script containing some characters linked to each prefab (e.g. 'B' for "BlueCube"). Each time it collects the corresponding character, and each time the code loops through my array, it gets the nex character and prints it into a .txt file.

My code is supposed to print a map. It starts by printing the first line (as expected), but it also print the rest of the lines below the first one. Logical, but it is not what I wanted ; the map is printed backwards.

So, I decided to read the last line of my array, THEN to decrement the index of my gridHeight, so that each line of my map is printed in the right order. And here comes the problem :

alt text

As you can see, my loops go through the first line correctly. But as soon as it starts looping through the second line, I get an error message. Here is my code (simplified, but the problem remains here) :

     private Transform[,] allObjectsInForegroundLayer;
     int Height = 5;
     int Width = 5;

 void Start(){
     allObjectsInForegroundLayer = new Transform[gridSize.gridSizeWidth, gridSize.gridSizeHeight];
     Place_Stuff_Here();
     Start_Looping();
 }
 
 void Place_Stuff_Here(){
     //Blablabla, I fill the array here. I know the problem doesn't come from here, since my map was actually filled with characters, even though it was printed backwards.
 }
 
 void Start_Looping(){
 
     int mapWidth = 0;
 
     for (int y = 0; y < Height; y++)
     {
       for (int x = 0; x < Width; x++)
       {
 
          print(x +", "+ (Height - 1 - y));
 
          if (i == LayerMask.NameToLayer("Foreground") && allObjectsInForegroundLayer.Length > 0)
          {
             if (allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name != "NoneForeground")
                 caracterToprint = index.ReturnReceivedChar(allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name.Replace("(Clone)", null));
             else
                 caracterToprint = index.ReturnReceivedChar(allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name);
          }
 
         file.Write(caracterToprint);
 
      }
 
      mapWidth += gridSize.gridSizeWidth;
 }
 

For once, I absolutely don't know where that error comes from, I have already managed to print my map backwards. Basically, what I am trying to do is to get the last line of my array (the Height variable), substracting 1 to it so that it remains inferior to the length of my array, then each time it loops through, it substracts Y, so that if I have a Height of 5, the new index to check will be 4, then 3, then 2, etc...

As you could see on my picture, it goes through the 4th index very well, but as soon as it goes through the 3rd, it prints me that error. I am really sure my array is completely filled, since I've set many Debug.Log() to check its content.

Thanks for your answer.

error.png (62.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

204 People are following this question.

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

Related Questions

IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int) 0 Answers

Instantiate Script Not Working 1 Answer

IndexOutOfRangeException: IndexOutOfRangeException: Array index is out of range. FollowTarget.Update () (at Assets/Scripts/FollowTarget.cs:29) 1 Answer

Sort a list of gameObjects and remove any duplicates one using vector3 positions 0 Answers

Create a 2D array (3 columns, 1000 rows)? 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