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. Dismiss Notice

WaveSpawner help after killing first wayv..

Discussion in 'Scripting' started by bonden55, Jan 6, 2018.

  1. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Hello, I have made a Script for the enemy to spawn, and in (waves) now i have a problem that when i kill the first wave the next wave dont spawn heres my script (rly new in Scripting and unity) plz help



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnerWave : MonoBehaviour {
    6.  
    7.     public Wave[] waves;
    8.     public Enemy enemy;
    9.  
    10.     Wave currentWave;
    11.     int currentWaveNumber;
    12.  
    13.     int enemiesRemainingToSpawn;
    14.     float nextSpawnTime;
    15.  
    16.     void Start()
    17.     {
    18.         NextWave();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
    24.         {
    25.             enemiesRemainingToSpawn--;
    26.             nextSpawnTime = Time.time + currentWave.timeBetweenSpawn;
    27.  
    28.             Enemy spawnedEnemy = Instantiate(enemy, Vector3.zero, Quaternion.identity) as Enemy;
    29.            
    30.         }
    31.     }
    32.  
    33.  
    34.  
    35.     void NextWave()
    36.     {
    37.         currentWaveNumber++;
    38.         currentWave = waves[currentWaveNumber - 1];
    39.  
    40.         enemiesRemainingToSpawn = currentWave.enemyCount;
    41.     }
    42.  
    43.     [System.Serializable]
    44.     public class Wave
    45.     {
    46.         public int enemyCount;
    47.         public float timeBetweenSpawn;
    48.     }
    49. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There was a post very similar recently and I tried to find it, but couldn't.
    In the other thread, one of the responders wrote a short script showing how an event could report back to the spawner gameobject that it has died. Then, in the manager, you keep track of when the wave is dead, and start your next wave.
    You could try that..

    You could put a variable on a script on the enemy that is for the spawner, and when it's killed, call a method on the spawning script that says the enemy count has been reduced by 1.

    In either scenario you need to track the current number of remaining enemies (that are alive), and when it hits zero, call your next wave method.
    It would also be a good idea to make sure you are not beyond the index of your wave array. :)

    Do you need more help beyond that?
     
  3. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Yeh tnx, but how sould i write it in code haha x)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did none of it make sense for what to write? :)
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is Enemy a script on the prefab that you spawn?

    Code (csharp):
    1.  
    2. // inside SpawnerWave
    3. int currentEnemies; // this would be incremented and decremented as you spawn/kill enemies
    4. /* note: this could be in the wave class, but assuming you can only have 1 wave active at a time, it doesn't matter */
    5. public void EnemeyDied() {
    6.     currentEnemies--;
    7.     if(enemiesRemainingToSpawn == 0 && currentEnemies <= 0) {
    8.         // you've met the criteria to spawn a new wave, all enemies are gone and none are left to spawn.
    9.       }
    10.  }
    11. // when you spawn the enemy.
    12. Enemy enemy = Instantiate(/*Whatever*/);
    13. enemy.spawnerWave = this;
    14.  
    15. // Then, when the enemy dies , on its script
    16. void DidDie() {
    17.    // whatever logic you have there.
    18.    spawnerWave.EnemyDied();
    19.   }
    20.  
    That's not perfect, perhaps, but hopefully that helps to give you an idea.
     
  6. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    This is my script for The health and death its for player and Enemys
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CharacterStats : MonoBehaviour {
    4.  
    5.     public int maxHealth = 100;
    6.     public int currentHealth { get; private set; }
    7.  
    8.     public Stat damage;
    9.     public Stat armor;
    10.  
    11.     private void Awake()
    12.     {
    13.         currentHealth = maxHealth;
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.T))
    19.         {
    20.             TakeDamage(10);
    21.         }
    22.     }
    23.  
    24.     public void TakeDamage (int damage)
    25.     {
    26.         damage -= armor.GetValue();
    27.         damage = Mathf.Clamp(damage, 0, int.MaxValue);
    28.  
    29.         currentHealth -= damage;
    30.         Debug.Log(transform.name + "takes" + damage + "damage.");
    31.  
    32.         if(currentHealth <= 0)
    33.         {
    34.             Die();
    35.         }
    36.     }
    37.  
    38.     public virtual void Die()
    39.     {
    40.         //Die in some way
    41.         // This Method is meant to be overwritten
    42.         Debug.Log(transform.name + "died");
    43.     }
    44.  
    45.  
    46. }
    Sould i put hte Script you wrote in to my Spawnerwayv script ?
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, cool. So, say on your enemy are you planning to subclass that then you could add the spawnerWave variable & override the 'Die' method.
    Otherwise, you could have a separate script with the spawnerWave variable and perhaps override the Die method to use the variable on the other script (which calls your spawner, to tell it you died.. which checks for the count.. etc lol) :)
     
  8. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Tnx rly mutch but ass stupid as i am i dont understand x) haha but if i get it some how and when i do it will prob be alot of help Tnx soo mutch :D
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey there, it's okay. If you are new, I would really suggest that you try searching for the most basic things.
    Use all that simple, and easy information to grow your experience and knowledge so you can piece together (in time) many, many things... :)

    Hopefully that makes sense, even if it may sound uninteresting at first lol.

    Anyways, maybe this thread will make a bit more sense to you as you get a little more familiar with C# , scripting, and Unity. :)

    Good luck with your game!
     
    bonden55 likes this.
  10. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    Just looking at this, I'm afraid I don't see any loop that would make your code repeat beyond the first wave.
    Essentially, NextWave is only being called one time, then once that wave isdone, the script is done--it's never getting information for the next wave.

    You're going to need a conditional statement (with your current implementation, probably an if/then) that will call NextWave again after the wave is done, and will continue doing that until there are no more waves left.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Something like that, but he was wanting the next wave to come when the currently spawned number of enemies in the wave was dead.. :)
     
  12. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    Oh!

    Well then add a counter to the wave info, and a public increment function.

    On enemies, during Destroy(), call the function.

    Have your spawn loop until that condition is met or exceeded.