1

So I'm trying to set up a system in a project where these spawn points will spawn targets that move towards the player and have to be destroyed before reaching a certain point, or its game over. Everything seems to be working fine except for one issue. The spawners don't stop spawning. They're supposed to do waves, spawning more enemies after each wave has been finished. I'm totally lost as to where the error might be. Small note, originally I had the spawn count be 3 times the enemyspawncount, and spawnCount would count down to 0, then jump to 2 and remain there.

Spawner script:

    var targetPrefab:Transform;
    var spawnCount = deathcounter.enemySpawnCount;

    function Start()
    {
        StartCoroutine("CoStart");
    }
 
    function CoStart() : IEnumerator
    {
        while (true)
             yield CoUpdate();
    }
 

    function CoUpdate(){

        spawnCount = deathcounter.enemySpawnCount;

        while(spawnCount > 0)
        {
            var target= Instantiate(targetPrefab, transform.position, transform.rotation);

            target.rigidbody.AddForce(Vector3.right * (deathcounter.enemySpawnCount *0.5 * 100));
    
            spawnCount = spawnCount - 1;
            Debug.Log("Spawn" + spawnCount);
    
            yield WaitForSeconds (5);
    
        }
        deathcounter.timeToSpawn = false;
    }

Target script:

    var spawnCount = deathcounter.enemyDeathCount;

    function OnTriggerEnter() { 
        Destroy (gameObject);
        deathcounter.enemyDeathCount = deathcounter.enemyDeathCount + 1;
    }

Death Counter script:

    static var enemyDeathCount = 0;
    static var enemySpawnCount = 1;
    static var timeToSpawn : boolean = true;

    function Update () {

        if(enemyDeathCount % 3 == 0 && enemyDeathCount != 0){
            timeToSpawn = true;
            enemySpawnCount = enemySpawnCount + 1;
        }

    }
3
  • You wrote "They're supposed to do waves, spawning more enemies after each wave has been finished." Do you mean "after the player has killed all the enemies in a wave?" If so, one strategy a player could use would be to leave one enemy remaining, and just avoid it. Because then another wave would never spawn. (I just thought of that while and wanted to point it out.) Commented Oct 20, 2014 at 21:00
  • Well yes, but if they don't hit all the enemies before they get too close, they lose the game. Commented Oct 20, 2014 at 21:04
  • Okay, I get it. Makes sense. Commented Oct 20, 2014 at 21:04

2 Answers 2

1

The issue could be in function CoUpdate(). The value of deathcounter.enemySpawnCount never gets reduced in that function. So if CoUpdate() gets called again, deathcounter.enemySpawnCount will still have the same value, and more enemy prefabs will get instantiated.

If that is the issue, and I'm not just misreading your code, you can solve that easily by setting deathcounter.enemySpawnCount after you set spawnCount:

spawnCount = spawnCount - 1;
deathcounter.enemySpawnCount = spawnCount;
Debug.Log("Spawn" + spawnCount);
Debug.Log("Spawn (double-check) " + deathcounter.enemySpawnCount);
Sign up to request clarification or add additional context in comments.

5 Comments

That wasn't far off. I decided to remove the line where it set spawnCount to be enemySpawnCount, and it works.
Cool! It's been a couple years since I used Unity3D, and I think there's a lot more to your code than the snippet you put in your question, so I'm glad my guess pointed you in the right direction. (If you feel like giving my answer an up-vote, I'd appreciate it. :) )
I would if I had the reputation to do so.
Oh! I didn't know that was a requirement. Oh well! Thanks anyway! Glad you got it working!
I didn't really, I just got it to stop infinitely spawning. Now it doesn't spawn at all after the first wave.
1

With much more mature eyes, I can look back and correct myself.

First I have to flip the order of these commands so they both trigger.

function OnTriggerEnter() { 
        
        deathcounter.enemyDeathCount = deathcounter.enemyDeathCount + 1;
        Destroy (gameObject);
    }

Second I have to redo how spawnCount is handled. It should be removed from the 'target' script, and given a set initial value, not set to another variable value. It should only be changed in the while loop with each iteration, and in the death counter script, inside the if statement, so it is set to be equal to the new enemySpawnCount value only when that if statement is true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.