- Home /
Argument out of range
Hi guys. I was following this tutorial for AI (http://youtu.be/KD0kLCZNQlc) and I'm changing it into C#, but I got an Argument out of range exception so i wanted to know how could I fix it. This is the script:
public List cells = new List();
public List doorsToCells = new List();
public List inmediateCells = new List();
public bool testForCells = true;
public float waitToTestCells = 2;
public int stage = 1;
[HideInInspector]
public bool doorOpen = true;
void Awake () { doorOpen = true;
GameObject[] celda = GameObject.FindGameObjectsWithTag("AIPathCell");
for(int i = 0; i <= celda.Length; i++)
cells.Add(celda[i]);
doorsToCells.Capacity = cells.Capacity;
testForCells = true;
waitToTestCells = 2;
stage = 1;
}
void Update () { if (testForCells && waitToTestCells <=0) {
foreach (GameObject inmediateCell in inmediateCells)
{
for (int i = 0; i <= cells.Count-1; i++)
{
if (cells[i] == inmediateCell)
doorsToCells[i] = 1;
}
}
for (stage = 2; stage <= cells.Count; stage++)
{
for (int i = 0; i <= cells.Count-1; i++)
{
if (doorsToCells[i] == stage -1)
foreach (GameObject checkDoor in cells[i].GetComponent().doors)
{
if (checkDoor != gameObject)
{
foreach (GameObject checkCell in checkDoor.GetComponent().inmediateCells)
{
for (int j = 0; j<= cells.Count-1;j++)
{
if (cells[j] == checkCell)
doorsToCells[j] = stage;
}
}
}
}
}
}
testForCells = false;
}
waitToTestCells -= 1;
}
Thanks! Sorry for my english, I'm spanish.
Replace your Awake() with this. Not sure if this helps, untested...
void Awake () {
for (int i=0; i<cells.Count; i++) {
doorsToCells.Add( cells[i] );
}
}
It didn't work cause cells in a GameObject list, i'll edit and put the whole script. Thanks, anyway.
yeah, I thought about that after posting. Ok, I pasted the script and it came up with a few errors. First ones are the lists need to know what type they are going to store. Something like this:
public List<GameObject> cells = new List<GameObject>();
public List<int> doorsToCells = new List<int>();
public List<GameObject> inmediateCells = new List<GameObject>();
Next error I'm getting is:
foreach (GameObject checkDoor in cells[i].GetComponent().doors)
and
foreach (GameObject checkCell in checkDoor.GetComponent().inmediateCells)
These lines need to change. you need to specify which component to get. I don't know how you have your scripts and components set up, but I guess they should look something like this (I made up "Room" and "ThisClass???" for example here):
foreach (GameObject checkDoor in cells[i].GetComponent<Room>().doors)
foreach (GameObject checkCell in checkDoor.GetComponent<ThisClass???>().inmediateCells)
Also, is your posted code your whole script? Is it inside a class?
Oh, just realized about that. The type for the lists and the components deleted when i copied the script here. (I'm new here so i don't really know how to put here the scripts properly).
But the exception I'm getting is in the doorsToCells, cause when i look at it in the inspector it keeps saying that its size is 0 and it doesn't set the values i set in the script.
And yeah, the script is in a class. Thanks a lot.
In Awake, you made a typo I think. I don't use <= when starting from 0 usually.
for(int i = 0; i <= celda.Length; i++)
to
for(int i = 0; i < celda.Length; i++)
But that didn't fix the doorsToOpen problem, so I replace capacity line.
for(int i = 0; i < celda.Length; i++)
cells.Add( celda[i] );
//doorsToCells.Capacity = cells.Capacity;
for(int i = 0; i < cells.Count; i++)
doorsToCells.Add( 0 );
Your answer
![](https://koobas.hobune.stream/wayback/20220613081700im_/https://answers.unity.com/themes/thub/images/avi.jpg)