Not instantiating more than one gameobject at once, no idea why

Hello, I have a little messy code, so sorry about that (which is one of the reasons I am probably getting this bug)

Basically I made a script, attached it to gameobject, and it instantiates/spawns my prefab, but no matter what variable I put in InvokeRepeating in its function, I still get fixed instantiating, basically there are circles falling down in my game and I want to control how many circles spawn and how often etc. I managed to do this before but something went wrong in this particular script, here are notable spots in script:

in Start:

InvokeRepeating("SpawnCooldownChange", spawnCooldownFreq, spawnCooldownFreq); // No matter what I put here in variable, it does not actually change, even though in inspector it is changed
InvokeRepeating("SpawnObjectiveCircle", 0.1f, baseSpawnRepeat);

in void SpawnObjectiveCircle():

    void SpawnObjectiveCircle() 
    {
        GameObject newGameObject;
        newGameObject = (GameObject)Instantiate(circles[Random.Range(0, 5)], defaultSpawn + new Vector3(0, 8, 0), transform.rotation);
        newGameObject.transform.parent = this.transform;
        list.Add(newGameObject);
        GravityForce(); // give gravity
        InvokeRepeating("DestroyOutOfView", 4, 3.0F);
...

in void DestroyOutOfView():

 void DestroyOutOfView() //It basically despawns everything that leaves screen.
    {
        foreach (GameObject CirclesObject in list)
        {
            if (CirclesObject.gameObject.transform.position.y <= -8)
            {
                CirclesObject.gameObject.SetActive(false);
            }
        }
    }

And this one is quite important one and probably cause of a bug, in void SpawnCooldownChange():

void SpawnCooldownChange()
    {
        if (baseSpawnRepeat > 2.7)
        {
            spawnCooldownFreq = 0.7f;
            baseSpawnRepeat -= 0.08f;
        }
        else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 1.6)
        { 
            spawnCooldownFreq = 0.5f;
            baseSpawnRepeat -= 0.16f;
        }
        else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 0.7)
        {
            spawnCooldownFreq = 0.4f;
            baseSpawnRepeat -= 0.18f;
        }
        else if (baseSpawnRepeat < 2.7 && baseSpawnRepeat > 0.25f) // FOR SOME REASON SAME ISSUE AGAIN, NO MATTER WHAT I GIVE, IT DOES 1 CIRCLE AT A TIME.
        {
            spawnCooldownFreq = 0.4f;
            baseSpawnRepeat -= 0.16f;
        }
    }

^^ Here I just adjust how fast/often will they spawn

So problem is, it actually waits like 5 seconds and then instantiates instead of baseSpawnRepeat variable.

I posted this kinda in a hurry so please do tell me if I made any mistakes or forgot something.

I guess I did something really stupid so, help would be greatly appreciated!

Just to check, but the object where the InvokeRepeating is, does it get deleted or marked inactive during play? Those are the only cases I can think of that would cause InvokeRepeating to stop. Also, I suspect that InvokeRepeating, once called, does not update the variables and they get locked in.
I could be wrong, but that’s what I suspect is going on.

Your can’t change the frequency of the invokes in an InvokeRepeating. It is permanently set to the value set while calling it.
You also seem to invoke DestroyOutOfView in sort of a recursive manner, repeating the invoke for each SpawnObjectiveCircle, leaving you with more and more repeating invokes as time goes by.
I suggest looking into coroutines where you can dynamically adjust the delays.

Hello, sorry for late bump.

I tried adapting my code to make it use StartCoroutine, but now everything seems to be messed up.

I think it’s because my coroutine only gets called once even though I do “yield return new WaitForSeconds(baseSpawnRepeat);”

Any idea what might be going wrong? I probably made some stupid mistake.

You need to loop within your coroutine.
Here’s an example:

public class LoopRoutineForever : Monobehaviour
{
    private void Start()
    {
        StartCoroutine(LoopForever());
    }
    private IEnumerator LoopForever()
    {
        // Loop forever while application is running
        while (Application.isPlaying)   
        {
                Debug.Log("Begin loop.");
            yield return new WaitForSeconds(1f);    // Wait a second.
                Debug.Log("Just waited a second, will wait one more...");
            yield return new WaitForSeconds(1f);    // Wait a second.
                Debug.Log("End loop.");
            yield return null;    // Wait one frame
        }
    }
}
1 Like

Thanks for reply! That example helped me fix my problem, its all good now.

1 Like