Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Why are 2 enemies spawning?

Discussion in 'Getting Started' started by kmo86, Mar 3, 2021.

  1. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
    Code (CSharp):
    1. public class SpawnManagerX : MonoBehaviour
    2. {
    3.     public GameObject enemyPrefab;
    4.     public GameObject powerupPrefab;
    5.  
    6.     private float spawnRangeX = 10;
    7.     private float spawnZMin = 15; // set min spawn Z
    8.     private float spawnZMax = 25; // set max spawn Z
    9.  
    10.     public int enemyCount;
    11.     public int waveCount = 1;
    12.  
    13.  
    14.     public GameObject player;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
    20.  
    21.         if (enemyCount == 0)
    22.         {
    23.             waveCount++;
    24.             SpawnEnemyWave(waveCount);
    25.         }
    26.  
    27.     }
    28.  
    29.     // Generate random spawn position for powerups and enemy balls
    30.     Vector3 GenerateSpawnPosition ()
    31.     {
    32.         float xPos = Random.Range(-spawnRangeX, spawnRangeX);
    33.         float zPos = Random.Range(spawnZMin, spawnZMax);
    34.         return new Vector3(xPos, 0, zPos);
    35.     }
    36.  
    37.  
    38.     void SpawnEnemyWave(int enemiesToSpawn)
    39.     {
    40.         Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end
    41.  
    42.         // If no powerups remain, spawn a powerup
    43.         if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
    44.         {
    45.             Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
    46.         }
    47.  
    48.         // Spawn number of enemy balls based on wave number
    49.         for (int i = 0; i < enemiesToSpawn; i++)
    50.         {
    51.             Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
    52.         }
    53.  
    54.      
    55.         ResetPlayerPosition(); // put player back at start
    56.  
    57.     }
    58.  
    59.     // Move player back to position in front of own goal
    60.     void ResetPlayerPosition ()
    61.     {
    62.         player.transform.position = new Vector3(0, 1, -7);
    63.         player.GetComponent<Rigidbody>().velocity = Vector3.zero;
    64.         player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    65.  
    66.     }
    67.  
    68. }
    69.  
     
  2. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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?
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Follow the logic:

    public int waveCount = 1; (waveCount = 1)
    waveCount++; (waveCount = 2)
    SpawnEnemyWave(waveCount); (spawning 2 enemies)
     
    Schneider21 likes this.
  4. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104

    But on the script from the tutorial before it I've got same similar code and it only spawns 1 enemy to start.
    Code (CSharp):
    1. public class SpawnManager : MonoBehaviour
    2. {
    3.     public GameObject powerupPrefab;
    4.     public GameObject enemyPrefab;
    5.     private float spawnRange = 9f;
    6.     public int enemyCount;
    7.     public int waveNumber = 1;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         SpawnEnemyWave(waveNumber);
    14.         Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
    15.     }
    16.  
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         enemyCount = FindObjectsOfType<Enemy>().Length;
    22.  
    23.         if(enemyCount == 0)
    24.         {
    25.             waveNumber++;
    26.             SpawnEnemyWave(waveNumber);
    27.             Instantiate(powerupPrefab, GenerateSpawnPosition(), powerupPrefab.transform.rotation);
    28.         }
    29.     }
    30.  
    31.     void SpawnEnemyWave(int enemiesToSpawn)
    32.     {
    33.         for (int i = 0; i < enemiesToSpawn; i++)
    34.         {
    35.             Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
    36.         }
    37.  
    38.         waveNumber++;
    39.     }
    40.     private Vector3 GenerateSpawnPosition()
    41.     {
    42.         float spawnPosX = Random.Range(-spawnRange, spawnRange);
    43.         float spawnPosZ = Random.Range(-spawnRange, spawnRange);
    44.  
    45.         Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
    46.  
    47.         return randomPos;
    48.     }
    49. }
    50.  
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,509
    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.
     
  6. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    I have changed it in game object script and it needs to be set as 0 to spawn 1 enemy.
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,509
    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?
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Same for enemyCount
     
  9. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    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://forum.unity.com/threads/tips-for-new-unity-users.701864/#post-5057741
     
    Joe-Censored likes this.
  11. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     
  12. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Likely changed enemyCount. You'll want to debug to see exactly why!
     
  13. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,509
    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.
     
  14. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
     
  15. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    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.
    Code (CSharp):
    1. public class SpawnManagerX : MonoBehaviour
    2. {
    3.     public GameObject enemyPrefab;
    4.     public GameObject powerupPrefab;
    5.  
    6.     private float spawnRangeX = 10;
    7.     private float spawnZMin = 15; // set min spawn Z
    8.     private float spawnZMax = 25; // set max spawn Z
    9.  
    10.     public int enemyCount;
    11.     public int waveCount = 1;
    12.  
    13.  
    14.     public GameObject player;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
    20.  
    21.         if (enemyCount == 0)
    22.         {
    23.             SpawnEnemyWave(waveCount);
    24.         }
    25.  
    26.     }
    27.  
    28.     // Generate random spawn position for powerups and enemy balls
    29.     Vector3 GenerateSpawnPosition ()
    30.     {
    31.         float xPos = Random.Range(-spawnRangeX, spawnRangeX);
    32.         float zPos = Random.Range(spawnZMin, spawnZMax);
    33.         return new Vector3(xPos, 0, zPos);
    34.     }
    35.  
    36.  
    37.     void SpawnEnemyWave(int enemiesToSpawn)
    38.     {
    39.         Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end
    40.  
    41.         // If no powerups remain, spawn a powerup
    42.         if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
    43.         {
    44.             Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
    45.         }
    46.  
    47.         // Spawn number of enemy balls based on wave number
    48.         for (int i = 0; i < enemiesToSpawn; i++)
    49.         {
    50.             Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
    51.         }
    52.  
    53.         waveCount++;
    54.         ResetPlayerPosition(); // put player back at start
    55.  
    56.     }
    57.  
    58.     // Move player back to position in front of own goal
    59.     void ResetPlayerPosition ()
    60.     {
    61.         player.transform.position = new Vector3(0, 1, -7);
    62.         player.GetComponent<Rigidbody>().velocity = Vector3.zero;
    63.         player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    64.  
    65.     }
    66.  
    67. }
    68.  
    I am now going to look at what differences there is.
     
  16. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    Finally worked it out. It was line 23. waveCount++; causing it to spawn the extra enemy.
     
  17. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446