My error comes from the if-statement checking the names of the colliders with my triangle objects’ names. I had an index out of range error before start using a for…in loop, but I keep on getting this error now.
“InvalidCastException: Cannot cast from source type to destination type.”
var availableMoves : int[] = new int[3];
var checkColliders : Collider[];
function DeterminePlayerMoves(pawnNumber : int)
{
checkColliders = Physics.OverlapSphere(pPawn[pawnNumber].transform.position, 1.6, -1);
var k = 0;
for(var i in checkColliders)
{
//This is for an index to check the names of the triangles against collider names
for(var j=0; j<=89; j++)
{
if(checkColliders[i].name == triManager.triangle[j].name)
{
if(j != pawnTracker.CheckPawnLocation("pPawn", pawnNumber))
{
availableMoves[k] = j;
k++;
}
}
}
}
}
I’ve spent some time trying different things out, googling the error, and reading about arrays. I don’t know what the issue is. I’ve been using arrays and for loops, with no problems until using a Collider array.
Is the problem checking a Collider.name with a GameObject.name? They are both strings, so what is the problem?
Thank you for that tip. MSDN library wasn’t as helpful as you were.
However, I encountered another problem. I am getting an array index is out of range error when I try to assign values to the availableMoves array. I did some testing, and there will be no more than 4 values assigned to the array. Here is an updated sample of my code.
var availableMoves = new int[4];
var moveIndex : int = 0;
var checkColliders : Collider[];
function DeterminePlayerMoves(pawnNumber : int)
{
checkColliders = Physics.OverlapSphere(pPawn[pawnNumber].transform.position, 1.6, -1);
var triIndex : int;
for(var hits in checkColliders)
{
for(triIndex=0; triIndex<=89; triIndex++)
{
if(hits.name == triManager.triangle[triIndex].name)
{
if(triIndex != pawnTracker.CheckPawnLocation("pPawn", pawnNumber))
{
availableMoves[0] = 2;
availableMoves[1] = 2;
availableMoves[2] = 2;
availableMoves[3] = 4;
//availableMoves[moveIndex] = triIndex;
//moveIndex++;
}
}
}
}
}
The 2 lines I commented out show how I was initially doing it. I then tried to manually set the values, and I get the same error. I am lost.