Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make enemies spawn again

Discussion in 'Scripting' started by Sajid, Jan 28, 2013.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Hey guys,

    I have a script here that is supposed to spawn enemies after a randomly generated amount of seconds, but it only works once. Here is my code:
    Code (csharp):
    1. #pragma strict
    2. var zombiePrefab : GameObject; //Zombie prefab to spawn
    3. var spawnController : GameObject; //Main controller to dictate how many to spawn
    4. var player : GameObject; //player
    5. var zombiesSpawned : int; //keeps track of how many zombies spawned to report back to controller
    6. var waitTime : int; //how long to wait until the next zombie is spawned
    7. var waitMin : int; // minimum time to wait
    8. var waitMax : int; //maximum time to wait
    9. var spawn : boolean; // tells to spawn or not
    10.  
    11. function Start () { //what happens at start
    12. waitMin = 3; //
    13. waitMax = 10;// ^ Sets initial wait times
    14. waitTime = Random.Range(waitMin,waitMax); //Chooses a random number inbetween min and max to set for waittime
    15. spawn = true; // Tells spawner to spawn
    16. }
    17.  
    18. function Update () { //constantly happening
    19. if(spawn) //if the spawn variable is true
    20. {
    21.     Spawn(); //call function Spawn
    22. }
    23. }
    24.  
    25. function SetSpawn() { // resets the spawn variable after spawning
    26.     if(spawn == false) // if spawn variable is false (happens after every spawn)
    27.     {
    28.         print("SetSpawn");
    29.         yield WaitForSeconds(waitTime);
    30.         print("Waited");
    31.         spawn = true; // makes it spawn again
    32.     }
    33. }
    34.  
    35. function NewWaitTime() { //resets the amount of time to wait to a random between min and max
    36.     waitTime = Random.Range(waitMin, waitMax); //sets waittime
    37. }
    38.  
    39. function Spawn() { //actually spawns Zombies
    40.     if(spawnController.GetComponent(SpawnTimer).roundDone == false)
    41.     {
    42.     Instantiate(zombiePrefab, transform.position, transform.rotation); //spawns zombie at location of spawner
    43.     zombiesSpawned +=1; //adds to spawned count to keep track to send later
    44.     NewWaitTime(); // resets wait time
    45.     spawn = false; //sets spawn to false (to make you wait until you spawn another zombie)
    46.     SetSpawn(); //Runs setspawn to restart the cycle
    47.    
    48. }  
    49. }

    What is supposed to happen is it spawns an enemy, then generates a new number and waits for that many seconds, and then spawns another one. I dont understand why its not working, but it spawns one, and then the spawn variable just stays true.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2.  
    3. function Start() {
    4.   Spawn();
    5. }
    6.  
    7.  
    8. function Spawn() {
    9.    //do the spawn stuff
    10.    ;
    11.  
    12.    //tell spawn to run again after a short wait
    13.    Invoke("Spawn", Random.Range(minWait, maxWait);
    14. }
    15.  
    16.  
    17.