InvokeRepeating stops repeating???? 4-5 instances of an object in array breaks it. why?

I am having a hard time getting my invokeRepeating () to working after it spawns 4-5 objects from an array. Comments?


var SpawnMax = 0;
var SpawnCount = 1;
var sphere : GameObject; 
var waitTime : float = 1.0; 
var lastp : int;
var counts : int = 0;

var puzzlepieces : GameObject[];

function Start(){
    PlayerPrefs.SetInt("SpawnMax", SpawnMax);
    PlayerPrefs.SetInt("SpawnCount", 0);            
    InvokeRepeating("Spawn", waitTime, waitTime); 
}

function Spawn() {

//  SpawnMax = PlayerPrefs.GetInt("SpawnMax");
    SpawnCount = PlayerPrefs.GetInt("SpawnCount");

    if (SpawnCount >= SpawnMax){
    }
    else {
            var arraymax = puzzlepieces.length;
            if (PlayerPrefs.GetInt("match") == 1){
                var killp = PlayerPrefs.GetInt("currentp");
                var arr = new Array(puzzlepieces);
                arr.RemoveAt(killp);
                puzzlepieces = arr;
                PlayerPrefs.SetInt("match", 0);
            }       
            myrandominstance = (Random.Range(0,arraymax));
            if (myrandominstance == lastp){
                myrandominstance = (Random.Range(0,arraymax));
            }
            else if (myrandominstance != lastp){
                PlayerPrefs.SetInt("currentp", myrandominstance);
                var i = PlayerPrefs.GetInt("currentp");
                sphere = puzzlepieces*;*
 *sphere = Instantiate(sphere, transform.position, transform.rotation);*
 *PlayerPrefs.SetInt("SpawnCount", SpawnCount);*
 *SpawnCount = SpawnCount+1;*
 *lastp = i;*
 *}* 
 *}*
*}*
*```*

It's unlikely that the problem is with InvokeRepeating, but with the way your function Spawn works. Put some Debug.Log lines in there (or use Unity 3's source-level debugger if you have it) and see what's really going on. I can see a couple of ways the function could be getting called and then do nothing.

1) Check to see what MaxSpawn is set to. Once it's been reached, the function returns without doing anything. So make sure its set to a number as high as you expect.

2) Possibly bug: The following code may not be doing what you expect:

    myrandominstance = (Random.Range(0,arraymax));
    if (myrandominstance == lastp){
        myrandominstance = (Random.Range(0,arraymax));
    }
    else if (myrandominstance != lastp){

It looks like it's trying to pick a random puzzle piece. However, if the piece is the same as last time (lastp) it's trying to pick another random number. The first oddity is that the new number may also be the same as lastp-- the code doesn't check. Secondly, the ELSE line prevents any piece from getting instantiated and the function will exit.

3) Items are getting deleted from the puzzlepieces array if some kind of match was made (I'm guessing here because that code isn't shown.) The array of pieces seems to get smaller and smaller and eventually nothing will spawn. I'm not sure if that's by design.

Probable bug: note that removing an item from the list means that the index you saved for the last piece spawned (lastp) will sometimes point to the wrong puzzle piece. Removing an item from the middle of the list changes the indexes of all the puzzle pieces that were after it in the array.

Also note also that when the list gets small, it becomes more and more likely that lastp will equal the random number. It is even possible that the final piece will never spawn, if lastp equals 0.

Do you have any timers in this or in other scripts that might be Destroying spawned items automatically or after a certain period of time? That timer could be messing things up.

I just had a similar issue with invokerepeating but when i turned off the destroy timers I had, the problem went away.