Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IndexOutOfRangeException: Index was outside the bounds of the array.

Discussion in 'Scripting' started by xBoromirx, Feb 6, 2021.

  1. xBoromirx

    xBoromirx

    Joined:
    Jan 17, 2021
    Posts:
    4
    Hello.
    Im tryng to load objects from a tex file with :
    id , position , rotaion, and scale
    the look like that:

    30 1397.1050 682.558 44.23022 0.0 0.0 -60.0 0.9400001


    the script is :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using System.Linq;
    6. using System;
    7. using UnityEditor;
    8. using System.Globalization;
    9.  
    10.  
    11. public class Lorencia_Object_Position : MonoBehaviour
    12. {
    13.     //File path: C:/Users/User/Unity Projects/Mu World System/Assets/Davias.txt
    14.  
    15.     public GameObject[] objects;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         //Read entire file that prints out each line one at a time
    21.         string[] reader = System.IO.File.ReadAllLines("C:/Users/miro/Desktop/Create with code/Lorencia try/Assets/Lorencia_Scripts/LorenciaText_OBJ.txt");
    22.         for (int i = 0; i < reader.Length; i++)
    23.         {
    24.             //Debug.Log("Primary key is " + i + ". The data is " + reader[i] + "\n");
    25.  
    26.             //Seperate string spaced with tab
    27.             char[] delimiters = new char[] { ' ' };
    28.             string[] parts = reader[i].Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    29.             for (int a = 0; a < parts.Length; a++)
    30.             {
    31.                 Debug.Log(parts[a]);
    32.             }
    33.  
    34.             //Convert strings to float
    35.            float[] flt = new float[parts.Length];
    36.             for (int x = 1; x < 4; x++)
    37.             {
    38.                 flt[x] = float.Parse(parts[x]);
    39.             }
    40.  
    41.             //id
    42.             int id = int.Parse(parts[0]);
    43.  
    44.             //pos
    45.             float posx = float.Parse(parts[1], CultureInfo.InvariantCulture) / 100;
    46. // coords are 255,255 thats why  /100
    47.             float posy = float.Parse(parts[2], CultureInfo.InvariantCulture) / 100;
    48.             float posz = float.Parse(parts[3], CultureInfo.InvariantCulture) / 100;
    49.  
    50.             //rot
    51.             float rotx = float.Parse(parts[4], CultureInfo.InvariantCulture);
    52.             if (rotx > -9.0 && rotx < 9.0)
    53.                 rotx = rotx + 0;
    54.             else
    55.                 rotx = rotx / 100;
    56.  
    57.             float roty = float.Parse(parts[6], CultureInfo.InvariantCulture);
    58.  
    59.             float index = roty;
    60.             if (index > 0 && index < 10)
    61.                 index = roty + 0;
    62.             if (index > 10 && index < 100)
    63.                 index = roty / 10;
    64.             if (index > 100 && index < 1000)
    65.                 index = roty / 100;
    66.             if (index > 1000 && index < 10000)
    67.                 index = roty / 1000;
    68.  
    69.             float rotz = float.Parse(parts[5], CultureInfo.InvariantCulture);
    70.  
    71.             //scale
    72.             float scale = float.Parse(parts[7], CultureInfo.InvariantCulture);
    73.  
    74.  
    75.             Instantiate(objects[id], new Vector3(posx, posy, posz), Quaternion.Euler(rotx, index, rotz), objects[id].transform.parent);
    76.             objects[id].transform.localScale = new Vector3(scale, scale, scale);
    77.         }
    78.     }
    79. }
    im getting :

    IndexOutOfRangeException: Index was outside the bounds of the array.
    Lorencia_Object_Position.Start () (at Assets/Lorencia_Scripts/Lorencia_Object_Position.cs:38)


    I dont understand why i get this error.

    Any suggestion how do i fix this?

    Best regards.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
  3. xBoromirx

    xBoromirx

    Joined:
    Jan 17, 2021
    Posts:
    4
    i got onjly 1 line int the text file and sixe of array 107. i cant understand why is happening
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Not a soul on this board knows your setup better than you.

    I'll copy/paste it again for you, in case you missed it in the post above:

    "If after all that you are baffled by why you are getting this error, and you are POSITIVE that there are enough items in your collection to satisfy your dereference operation, then you need to put a Debug.Log() call right before the point where you get the error, and print out two things: the index you are dereferencing, and the Count (or Length) of the collection itself."

    Print the size out, and print the index out. I guarantee you will be amazed.
     
  5. GameBam

    GameBam

    Joined:
    Feb 4, 2021
    Posts:
    11
    Seeing the text file would give clues as to why this is happening. The error is caused by the for loop on line 38 trying to access an index that does not exist in flt[] or parts[]. This is most likely because a line in the file is not formatted as you expected and is missing data or the spaces between the data.

    Expected Data: 30 1397.1050 682.558 44.23022 0.0 0.0 -60.0 0.9400001

    Broken Data: 30 1397.1050 (Data entries are missing)
    Broken Data: 301397.1050682.55844.230220.0 0.0-60.00.9400001 (The spaces are missing)
     
    xBoromirx likes this.
  6. xBoromirx

    xBoromirx

    Joined:
    Jan 17, 2021
    Posts:
    4
    Hello.

    I used :

    1. //Seperate string spaced with tab
    2. char[] delimiters = new char[] { ' ' };

    for the empty space. How do you suggest to i make it? im srry im curently learning.
     
  7. xBoromirx

    xBoromirx

    Joined:
    Jan 17, 2021
    Posts:
    4
    FIxed!! thanks GameDev for pointing me the write way :). i used / for separating them and it worked