IndexOutOfRangeException: Index was outside the bounds of the array. RowManager.GenerateRow (System.Single hbias, System.Single vbias) (at Assets/Scripts/RowManager.cs:155) GameManager.Update () (at Assets/Scripts/GameManager.cs:38) The array has five cells and I index it from 0 to 4. Unity 2021.3.8f LTS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
class Baze
{
private byte Start,End;
public Baze()
{
Start = 0;
End= 0;
}
public void SetStart(byte s)
{
Start = s;
}
public void SetEnd(byte s)
{
End = s;
}
public byte GetStart()
{
return Start;
}
public byte GetEnd()
{
return End;
}
public void Correction()
{
if(Start>End)
{
byte temp = Start;
Start = End;
End = temp;
}
}
public bool isSingular()
{
return Start == End;
}
}
class MazeLogic
{
private List<SortedSet<byte>> Groups;
private SortedSet<byte> TempSet;
private Baze TempRange;
private List<Baze> Template;
private uint[] IDs;
private bool[] Hjoints, Vjoints;
public MazeLogic()
{
Groups = new List<SortedSet<byte>>();
TempRange = new Baze();
IDs = new uint[5] {1,2,3,4,5};
Hjoints = new bool[5] {false,false,false,false,false};
Vjoints = new bool[5] {true,true,true,true,true};
TempSet = new SortedSet<byte>();
Template = new List<Baze>();
for(byte i=0;i<4;i++)
{
TempRange.SetStart(i);
TempRange.SetEnd((byte)(i + 1));
Template.Add(TempRange);
}
}
private void SetGroups(float bias)
{
uint temp = 0;
for(byte i=0;i<5;i++)
{
Hjoints[i]= false;
if (Vjoints[i])
temp = IDs[i];
else
IDs[i] = 0;
}
for (byte i = 0; i < 5; i++)
if (IDs[i] == 0)
IDs[i] = ++temp;
TempRange.SetStart(0);
for(byte i=0;i<4;i++)
{
if (Random.value > bias && IDs[Template[i].GetStart()] != IDs[Template[i].GetEnd()])
{
TempSet.Add(Template[i].GetStart());
TempSet.Add(Template[i].GetEnd());
}
else
{
TempSet.Add(Template[i].GetStart());
Groups.Add(TempSet);
TempSet.Clear();
}
}
for (byte i = 0; i < Groups.Count; i++)
for (byte j = Groups[i].Min; j < Groups[i].Max; j++)
Hjoints[j] = true;
}
private void SetVjoints(float bias)
{
bool check;
for (byte i = 0; i < Groups.Count; i++)
{
check = false;
for (byte j = Groups[i].Min; j <= Groups[i].Max; j++)
if(Random.value>bias)
{
check = true;
Vjoints[j] = true;
}
if (!check)
Vjoints[Random.Range(Groups[i].Min, Groups[i].Max + 1)] = true;
}
}
public void GenerateMaze(float hbias,float vbias)
{
SetGroups(hbias);
SetVjoints(vbias);
}
public bool[] GetHorizontalJoints()
{
return Hjoints;
}
public bool[] GetVerticalJoints()
{
return Vjoints;
}
}
public class RowManager : MonoBehaviour
{
[SerializeField] GameObject[] Hwalls;
[SerializeField] GameObject[] Vwalls;
MazeLogic ml;
void Awake()
{
ml = new MazeLogic();
}
void Start()
{
Hwalls = new GameObject[4];
Vwalls = new GameObject[5];
for (byte i = 0; i < 4; i++)
Hwalls[i] = transform.GetChild(i).gameObject;
for (byte i = 0; i < 5; i++)
Vwalls[i] = transform.GetChild((byte)(i+4)).gameObject;
}
public void GenerateRow(float hbias,float vbias)
{
ml.GenerateMaze(hbias,vbias);
for (byte i = 0; i < 4; i++)
if (ml.GetHorizontalJoints()[i])
Hwalls[i].SetActive(false);
for(byte i=0;i<5;i++)
if (ml.GetVerticalJoints()[i])
Vwalls[i].SetActive(false);//line 155
}
public void Reset()
{
for (byte i = 0; i < 4; i++)
Hwalls[i].SetActive(true);
for (byte i = 0; i < 5; i++)
Vwalls[i].SetActive(true);
}
}
and array is also not null enter image description here