i have error: IndexOutOfRangeException: Array index is out of range.
using UnityEngine;
using System.Collections;
public class Zadanie_3 : MonoBehaviour {
int[,] tablica_2D = new int[10,10];
// 4x2x7
void Start ()
{
for(int a = 0; a < tablica_2D.Length; a++)
{
for(int b = 0; b < tablica_2D.Length; b++)
{
Debug.Log(tablica_2D[a,b]);
}
}
}
}
Because your array is multi-dimensional, you need to use Array.GetLength instead of just Length.
Your 2D array has two different lengths, one per dimension. Even if they’re the same. If they’re always 10, then you could just use a constant value. But in general, you’d need to use something like
for(int a = 0; a < tablica_2D.GetLength(0); a++)
{
for(int b = 0; b < tablica_2D.GetLength(1); b++)
{
Debug.Log(tablica_2D[a,b]);
}
}
Length refers to the total array length, which would be 10 * 10 = 100 in this case. That’s why you went out of bounds.