Why are 2 enemies spawning?

on task 6 of Challenge 4 - Soccer Scripting - Unity Learn I have got it so 1 more enemy spawns in each wave, but the first wave should have 1 enemy but 2 spawn.
This is my code I can’t understand why 2 spawn and how to get 1 to spawn.

public class SpawnManagerX : MonoBehaviour
{
    public GameObject enemyPrefab;
    public GameObject powerupPrefab;

    private float spawnRangeX = 10;
    private float spawnZMin = 15; // set min spawn Z
    private float spawnZMax = 25; // set max spawn Z

    public int enemyCount;
    public int waveCount = 1;


    public GameObject player;

    // Update is called once per frame
    void Update()
    {
        enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;

        if (enemyCount == 0)
        {
            waveCount++;
            SpawnEnemyWave(waveCount);
        }

    }

    // Generate random spawn position for powerups and enemy balls
    Vector3 GenerateSpawnPosition ()
    {
        float xPos = Random.Range(-spawnRangeX, spawnRangeX);
        float zPos = Random.Range(spawnZMin, spawnZMax);
        return new Vector3(xPos, 0, zPos);
    }


    void SpawnEnemyWave(int enemiesToSpawn)
    {
        Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end

        // If no powerups remain, spawn a powerup
        if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
        {
            Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
        }

        // Spawn number of enemy balls based on wave number
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
        }

      
        ResetPlayerPosition(); // put player back at start

    }

    // Move player back to position in front of own goal
    void ResetPlayerPosition ()
    {
        player.transform.position = new Vector3(0, 1, -7);
        player.GetComponent<Rigidbody>().velocity = Vector3.zero;
        player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

    }

}

I have changed the waveCount to 0 and it then spawns 1 enemy. So why is it spawning 1 extra enemy than the number says?

Follow the logic:

public int waveCount = 1; (waveCount = 1)
waveCount++; (waveCount = 2)
SpawnEnemyWave(waveCount); (spawning 2 enemies)

1 Like

But on the script from the tutorial before it I’ve got same similar code and it only spawns 1 enemy to start.

public class SpawnManager : MonoBehaviour
{
    public GameObject powerupPrefab;
    public GameObject enemyPrefab;
    private float spawnRange = 9f;
    public int enemyCount;
    public int waveNumber = 1;


    // Start is called before the first frame update
    void Start()
    {
        SpawnEnemyWave(waveNumber);
        Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
    }
  

    // Update is called once per frame
    void Update()
    {
        enemyCount = FindObjectsOfType<Enemy>().Length;

        if(enemyCount == 0)
        {
            waveNumber++;
            SpawnEnemyWave(waveNumber);
            Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
        }
    }

    void SpawnEnemyWave(int enemiesToSpawn)
    {
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
        }

        waveNumber++;
    }
    private Vector3 GenerateSpawnPosition()
    {
        float spawnPosX = Random.Range(-spawnRange, spawnRange);
        float spawnPosZ = Random.Range(-spawnRange, spawnRange);

        Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);

        return randomPos;
    }
}

Where are you changing waveNumber? Since you have it set as a public variable, Unity has probably serialized the value you had assigned initially. So if you’re changing it in the code now, the game is still using the value you had before. Select the GameObject the script is assigned to and check/change the value of waveNumber there.

I have changed it in game object script and it needs to be set as 0 to spawn 1 enemy.

I don’t understand what you mean. Did you change the line in code? Or did you change the value in the Inspector?

Put another way: when you select the object with this script attached to it, what does the Inspector say for the property Wave Number?

Same for enemyCount

I’ve changed it in the inspector not in the actual script. If I change it in the script same thing happens. When I press play the number in inspector adds 1 to what I set it to. So it does spawn the number of enemies the inspector but it spawns 1 more than I set it to.

That is exactly what your code is doing if enemyCount is 0, see my previous message. Please share a screenshot of your Inspector settings. You can use Debug.Log to confirm https://discussions.unity.com/t/748729/14

1 Like

Very strange I did the next step to get enemy balls to roll towards the players goal and it started spawning the correct number of enemies. I have no idea why.

Likely changed enemyCount. You’ll want to debug to see exactly why!

The worst thing you could do for yourself right now would be to move on without understanding why what’s happening is happening. This is a great opportunity to learn a practical understanding from an actual experience. Keep digging at it until you understand how it works! Otherwise you’ll soon find yourself in the same situation when the next obstacle arises.

that makes sense but I’m near enough certain that it just didn’t work at first but does now. I know doesn’t make sense so later when I go on it later I’ll post the code that I did last night which worked straight away to see if anyone can see any differences in it.

Ok so I am missing something in the script last night that caused it to spawn 2 enemies instead of 1. So just to clarify this script here is the correct script to spawn 1 enemy the scripts in first post spawns 2.

public class SpawnManagerX : MonoBehaviour
{
    public GameObject enemyPrefab;
    public GameObject powerupPrefab;

    private float spawnRangeX = 10;
    private float spawnZMin = 15; // set min spawn Z
    private float spawnZMax = 25; // set max spawn Z

    public int enemyCount;
    public int waveCount = 1;


    public GameObject player;

    // Update is called once per frame
    void Update()
    {
        enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;

        if (enemyCount == 0)
        {
            SpawnEnemyWave(waveCount);
        }

    }

    // Generate random spawn position for powerups and enemy balls
    Vector3 GenerateSpawnPosition ()
    {
        float xPos = Random.Range(-spawnRangeX, spawnRangeX);
        float zPos = Random.Range(spawnZMin, spawnZMax);
        return new Vector3(xPos, 0, zPos);
    }


    void SpawnEnemyWave(int enemiesToSpawn)
    {
        Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end

        // If no powerups remain, spawn a powerup
        if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
        {
            Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
        }

        // Spawn number of enemy balls based on wave number
        for (int i = 0; i < enemiesToSpawn; i++)
        {
            Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
        }

        waveCount++;
        ResetPlayerPosition(); // put player back at start

    }

    // Move player back to position in front of own goal
    void ResetPlayerPosition ()
    {
        player.transform.position = new Vector3(0, 1, -7);
        player.GetComponent<Rigidbody>().velocity = Vector3.zero;
        player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

    }

}

I am now going to look at what differences there is.

Finally worked it out. It was line 23. waveCount++; causing it to spawn the extra enemy.

As stated previously https://discussions.unity.com/t/831182/3