- Home /
For loop not repeating - (JAVASCRIPT)
Hello, everyone. I'm working on a memry card game and using the "iphone Game 4.0" code as a base. (it's a memory card game...)
Whenever I run the code, it doesn't create the cards because of this error:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
I've looked on the forums and cannot find a satisfactory answer - I've even copy-and-pasted the code from the old script intO my project file.
Here is the code:
#pragma strict
var cardLocations = new Array
(
Vector3 (0, 0, 0), Vector3(3, 0, 0), Vector3(6, 0, 0), Vector3(9, 0, 0), Vector3(12, 0, 0), Vector3(15, 0, 0),
Vector3 (0, 0, -4), Vector3(3, 0, -4), Vector3(6, 0, -4), Vector3(9, 0, -4), Vector3(12, 0, -4), Vector3(15, 0, -4),
Vector3 (0, 0, -8), Vector3(3, 0, -8), Vector3(6, 0, -8), Vector3(9, 0, -8), Vector3(12, 0, -8), Vector3(15, 0, -8)
);
var allCards : GameObject;
var parentObject : GameObject;
var usedCardLocation;
var uniqueCard = new Array ();
function Start () {
{
// Find all Game Objects that are Tagged "Cards"...
uniqueCard = allCards.FindGameObjectsWithTag ("Cards");
// Randomize the order of the Cards...
for ( var c=0; c <= uniqueCard.length-1; c++)
{
var thisCard = Random.Range (c, uniqueCard.length);
usedCardLocation = uniqueCard [c];
uniqueCard [c] = uniqueCard [thisCard];
uniqueCard [thisCard] = usedCardLocation;
}
// Randomize the order of the Card Locations...
for ( var i=0; i <= cardLocations.length-1; i++)
{
var thisCardLocation = Random.Range (i, cardLocations.length);
usedCardLocation = cardLocations [i];
cardLocations [i] = cardLocations [thisCardLocation];
cardLocations [thisCardLocation] = usedCardLocation;
// Instantiate our Randomized Cards and parent them to empty Game Objects so the animations play in Local Space...
var theCard : GameObject = Instantiate (uniqueCard [i/2], Vector3(0,0,0), Quaternion.identity);
var theParent : GameObject = Instantiate (parentObject, Vector3(0,0,0) , Quaternion.identity);
theCard.transform.parent = theParent.transform;
theParent.transform.position = cardLocations [i];
}
// Destroy the group of cards that we're using to generate the Card Locations...
Destroy(allCards, 0.1);
}
}
Can somebody please help me?
Thanks!
-Sam
I believe the index that is out of range isn't an issue with the for loop itself. Your issue is where you're using your iterator to call a value. It's trying to use an index that doesn't exist in one of the arrays It's just some simple logic. Just run through all of your code and think hard about the possible size of each array, and see if the 'i' or 'c' is larger than a possible index in each array. I would search myself, but I'm too tired to find that.
Can you past in the error message from the console, and/or indicate which line is giving you the error? Note the Array class is to be avoided. Use a built-in array ins$$anonymous$$d.
This WILL throw this exception if uniqueCard.length is zero, or I think, less than half the size of cardLocations. Perhaps that array isn't filling up in the first place. Do a debug.log( uniqueCard.length ); and see if it's found all the game objects okay
Answer by GrahamReeves · Apr 10, 2014 at 09:40 AM
I created a scene with a parent object, and 3 cubes tagged with "Cards" and dropped your code in and it stopped with "parentObject is unassigned".
If I removed the Cards tags, I get the error ArgumentOutOfRangeException
like you do. So perhaps you don't have the tags setup correctly? link text