- Home /
IndexOutOfRange Issue
When I enter play mode, I keep getting the following error in the console:
IndexOutOfRangeException: Index was outside the bounds of the array. Grids2D.Sprites.PlaceSprites () (at Assets/Sprites.cs:90) Grids2D.Sprites.Start () (at Assets/Sprites.cs:57)
Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Grids2D;
using System;
using Random = UnityEngine.Random;
namespace Grids2D {
public class Sprites : MonoBehaviour
{
public GameObject redBall;
public Grid2D grid;
private float[] xSpriteCoords;
private float[] ySpriteCoords;
private int numberOfSprites = 5000;
TERRAIN[] terrainAssignments;
public enum TERRAIN
{
Open = 0,
LightWater = 1,
LightWater_Road = 2,
HeavyWater = 3,
HeavyWater_Road = 4,
LightWood = 5,
LightWood_Road = 6,
HeavyWood = 7,
HeavyWood_Road = 8,
Village = 9,
Road = 10,
LightHill = 11,
LightHill_Road = 12,
HeavyHill = 13,
HeavyHill_Road = 14,
Impassable = 15,
HeavyWater_HeavyHill = 16,
HeavyHill_Village = 17,
HeavyWater_Village = 18,
LightHill_LightWood = 19,
HeavyHill_HeavyWood = 20,
HeavyWater_HeavyWood = 21,
Road_Impassable = 22,
Impassable_Impassable = 23,
HeavyWater_Impassable = 24,
LightWood_HeavyWood = 25
}
void Start()
{
Debug.Log("started");
xSpriteCoords = RandomPointCoords();
ySpriteCoords = RandomPointCoords();
Debug.Log("randomized coordinate arrays");
//Instantiate(redBall, new Vector3(xSpriteCoords[5], ySpriteCoords[5]), Quaternion.identity);
terrainAssignments = TerrainAssignments(); //assign the values of the terrains array with AssignTerrain
Debug.Log("generated terrain assignments");
PlaceSprites();
Debug.Log("placed sprites");
Debug.Log("ended");
}
private float[] RandomPointCoords()
{
float[] array = new float[numberOfSprites];
for (int i = 0; i < numberOfSprites; i++)
{
array[i] = UnityEngine.Random.Range(-250f, 250f);
}
return array;
}
private TERRAIN[] TerrainAssignments() { //returns an array of terrain assignments corresponding to ammount of cells
TERRAIN[] array = new TERRAIN[grid.numCells];
Debug.Log(array.Length);
for (int i = 0; i < grid.numCells; i++)
{
array[i] = (TERRAIN)Random.Range(0, 25); //sets terrain type to something completely random
}
return array;
}
private void PlaceSprites()
{
TERRAIN terrain;
for (int i = 0; i < numberOfSprites; i++)
{
Debug.Log(i);
Debug.Log("terrainAssignments.Length = " + terrainAssignments.Length);
Debug.Log("xSpriteCoords.Length = " + xSpriteCoords.Length);
terrain = terrainAssignments[grid.CellGetIndex(grid.CellGetAtPosition(new Vector3(xSpriteCoords[i], ySpriteCoords[i])))]; //gets what terrain is at the random coordinate
if ((terrain == TERRAIN.HeavyWood) | (terrain == TERRAIN.HeavyWood_Road) | (terrain == TERRAIN.LightWood_HeavyWood)) //if it is heavy wood
{
Instantiate(redBall, new Vector3(xSpriteCoords[i], ySpriteCoords[i]), Quaternion.identity);
//Debug.Log("instantiated sprite");
}
}
}
}
}
According to my math, no index should be outside the bounds of the array at terrainAssignments array. Could someone help point out what I'm missing? If it helps, numCells is 30.
Thank you for your time.
Answer by logicandchaos · Jan 29, 2020 at 08:20 PM
This line is where it goes out of index: terrain = terrainAssignments[grid.CellGetIndex(grid.CellGetAtPosition(new Vector3(xSpriteCoords[i], ySpriteCoords[i])))]; I think this is causing the issue tho.. for (int i = 0; i < numberOfSprites; i++), your loop runs 5000 times but your grid is 30x30 or 900 tiles..
There are only 30 tiles, but it's not the tiles it's counting -- it's the 2 spriteCoords arrays, which each should have 5000 elements, because of lines 51 and 52.
It sounds like you have made a simple off by one error