OverlapSphere, Arrays, and Errors - Oh My!

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?

You have

for(var i in checkColliders)

but then you do

if(checkColliders[i].name

–Eric

If I use:

for(var i=0; i<=checkColliders.length; i++)

I get index out of range error. That for…in loop was just trying something else out. I guess that wont work.

The issue still lies within the if statement. How does my index get out of range with that for loop I above that I was initially using?

If you’re using for/in, then you’d do

if (i.name

and not

if(checkColliders[i].name

since i is not an integer index.

–Eric

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.

Do a “print (availableMoves.Length);” to see what you’re working with.

–Eric

Yea I just had to make my one line array declaration into two lines.

var availableMoves : int[ ];
availableMoves = new int[4];

Weird, and this took me several hours to figure out. I don’t know why I didn’t Debug.Log the length value earlier. Thanks again…