Question by
123mcole · Feb 25, 2016 at 10:33 AM ·
c#arrayout of range
Array Index Out of bounds, line error stated;
Line error is 197; I have no clue why, as i set there value up the first time, anything above 150 does not work, no clue?
using UnityEngine; using System.Collections;
public class GrassGaneration : MonoBehaviour {
public GameObject[] blockidset = new GameObject[3];
public Transform grasstrans;//-1 = air(nothing), 0 = grass, 1 = dirt, 2 = stone
public Transform dirttrans;
private int count;
public int lastlongelevation;
private int[] grassmap; //= new int[50];
private int[] elevationmap; //= new int[50];
private int[,] blockid; //= new int [50,100];
public int elevationnoise;
public int elevationscalenoise;
//private int randommineral;
//private int oresizex;
//private int oresizey;
//private int[] orenoise = new int[40];
//private int scaleore;
//private int randomore2;
//private int orechange;
private int numbercount = 1;
private int max;
public int maplength;
void Start()
{
grassmap = new int[maplength];
elevationmap = new int[maplength];
blockid = new int[maplength, 100];
setblockidtozero();
grassmapcreate();
grassmapsmooth();
creatorelevationmap();
grassmapelevationmapcombine();
findmaxgrassmapvalue();
setblockid();
createblocks();
}
// Update is called once per frame
void Update()
{
}
void creatorelevationmap() {
int chanceofchange = 0;
int scale = 0;
int updown = 0;
int length = 0;
for (int q = 0; q < maplength; q++)
{
if (chanceofchange == 1)
{
length = (int)Mathf.Round(Random.Range(Mathf.Round(lastlongelevation/2), lastlongelevation));
scale = (int)Mathf.Round(Random.Range(0, elevationscalenoise));
updown = (int)Mathf.Round(Random.Range(0, 2)) - 1;
chanceofchange = 0;
}
if (length == 0)
{
chanceofchange = (int)Mathf.Round(Random.Range(0, elevationnoise));
scale = 0;
}
else if (length != 0)
{
scale = (int)Mathf.Round(Random.Range(0, elevationscalenoise));
length = length - 1;
}
if (q == 0) {
if (updown == 0)
{
elevationmap[q] = scale;
}
else {
elevationmap[q] = -scale;
}
} else {
if (updown == 0)
{
elevationmap[q] = elevationmap[q - 1] + scale;
}
else {
elevationmap[q] = elevationmap[q - 1] - scale;
}
}
if (elevationmap[q] + grassmap[q] < 19)
{
updown = 0;
}
}
}
void setblockidtozero()
{
for (int bb = 0; bb < maplength; bb++)
{
for (int bv = 0; bv < 100; bv++)
{
blockid[bb, bv] = -1;
}
}
}
void grassmapcreate()
{
grassmap[0] = (int)Mathf.Round(Random.Range(0, 3)) - 1;
for (int c = 1; c < maplength; c++)
{
numbercount++;
count = (int)Mathf.Round(Random.Range(0, 3));
if (grassmap[c - 1] == -4)
{
grassmap[c] = grassmap[c - 1] + count;
}
else if (grassmap[c - 1] == 4)
{
grassmap[c] = grassmap[c - 1] + count - 2;
}
else {
grassmap[c] = grassmap[c - 1] + count - 1;
}
}
}
void grassmapsmooth()
{
for (int u = 1; u < maplength - 1; u++)
{
if (grassmap[u - 1] == grassmap[u + 1])
{
if (grassmap[u] + 1 == grassmap[u - 1])
{
grassmap[u] = grassmap[u - 1];
}
else if (grassmap[u] - 1 == grassmap[u - 1])
{
grassmap[u] = grassmap[u - 1];
}
}
}
}
void grassmapelevationmapcombine()
{
for (int y = 0; y < maplength; y++)
{
grassmap[y] = grassmap[y] + elevationmap[y];
}
}
void findmaxgrassmapvalue()
{
max = grassmap[0];
for (int p = 1; p < grassmap.Length; p++)
{
if (grassmap[p] > grassmap[p - 1])
{
max = grassmap[p];
}
}
}
void setblockid()
{
for (int b = 0; b < maplength; b++)
{
blockid[b, grassmap[b] + 60] = 0;
for (int n = 0; n < (grassmap[b] + 60); n++)
{
if (n > 40)
{
blockid[b, n] = 1;
}
else {
blockid[b, n] = 2;
}
}
}
}
void createblocks()
{
for (int b1 = 0; b1 < maplength; b1++)
{
for (int n1 = 0; n1 < 100; n1++)
{
if (blockid[b1, n1] == -1)
{
}
else {
Instantiate(blockidset[blockid[b1, n1]], new Vector2(8 * b1, (n1 - 60) * 8), dirttrans.rotation);
}
}
}
}
}
put this into your code editor to see the line numbers, i couldn't get "printscreen" to work on here.
Comment
for (int n = 0; n < (grassmap[b] + 60); n++)
{
if (n > 40)
{
blockid[b, n] = 1;
}
In the beginning you say blockid = new int[maplength, 100];
when n is n is above 100 you're trying to access an element that doesn't exist. It's out of the range of the array. So whenever grassmap[b] is above 40 you will try to access n > 100 and your array isn't that big.