I am getting an error immediately when I start:
IndexOutOfRangeException: Array index is out of range.
I understand you don’t need to initialize to 0. It gives same error if I try to initialize to another int. Can anybody tell me what I am doing wrong?
using UnityEngine;
using System.Collections;
public class simpleMultiDarrays : MonoBehaviour {
int[,] xyb_count_array = new int[6, 6];
int[,] xyr_count_array = new int[6, 6];
void Start()
{
int inity = 0;
for (inity = 0; inity < 6; inity++)
{
int initx = 0;
for (initx = 0; initx < 6; initx++)
{
xyb_count_array[initx, inity] = 0;
xyr_count_array[initx, inity] = 0;
}
}
}
}
TBruce
2
@jfrohbach
Replace your code with this, it will tell you were your error is
using UnityEngine;
using System.Collections;
public class simpleMultiDarrays : MonoBehaviour
{
int[,] xyb_count_array = new int[6, 6];
int[,] xyr_count_array = new int[6, 6];
void Start()
{
if ((xyb_count_array != null) && (xyr_count_array != null))
{
Debug.Log("xyb_count_array.Length = " + xyb_count_array.Length + ", xyr_count_array.Length = " + xyr_count_array.Length);
for (int inity = 0; inity < 6; inity++)
{
for (int initx = 0; initx < 6; initx++)
{
string error = "inity = " + inity + ", initx = " + initx + ", ";
if (xyb_count_array[initx, inity] == null)
error += "xyb_count_array[" + initx + ", " + inity "] = null, ";
else
xyb_count_array[initx, inity] = 0;
if (xyr_count_array[initx, inity] == null)
error += "xyr_count_array[" + initx + ", " + inity "] = null, ";
else
xyr_count_array[initx, inity] = 0;
Debug.Log(error);
}
}
}
else
{
string error = "";
if (xyb_count_array == null)
error = "xyb_count_array == null, ";
if (xyr_count_array != null)
error += "xyr_count_array == null";
Debug.Log(error);
}
}
}
Thank you for your answer.
I found error in my syntax. My code in unity varied very slightly from the sample I posted.
I put the following debug line in my code and was able to find where it was producing the error.
xyb_count_array[initx, inity] = 0;
xyr_count_array[initx, inity] = 0;
Debug.Log("Setting position X" + initx + " Y" + inity + " to 0");
}