Search Unity

spawning a boss

Discussion in '2D' started by hadou357, Sep 20, 2018.

  1. hadou357

    hadou357

    Joined:
    Aug 28, 2018
    Posts:
    22
    Hello I've been working on this C# strip for last few days trying to figure out how to spawn 1 unit. I figured out how to spawn many units as a continuous loop but I don't know how to spawn just one unit at a Pacific time I'm hoping that there is someon
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spanwer : MonoBehaviour {
    6.  
    7.     public GameObject Enemy;
    8.     public Transform[] spawnSpots;
    9.     private float timeBtwSpawns;
    10.     public float startTimeBtwSpawns;
    11.  
    12.     private void Start(){
    13.         timeBtwSpawns = startTimeBtwSpawns;
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if(timeBtwSpawns <= 0){
    19.             int randPos = Random.Range(0, spawnSpots.Length);
    20.             Instantiate(Enemy, spawnSpots[randPos].position, Quaternion.identity);
    21.             timeBtwSpawns = startTimeBtwSpawns;
    22.         }else{
    23.             timeBtwSpawns -= Time.deltaTime;
    24.         }
    25.     }
    26.  
    27. }
    28.  
    e out there that could help me with this small issue any and all help is welcome.
     
  2. OfficeThrashing

    OfficeThrashing

    Joined:
    May 18, 2018
    Posts:
    13
    Code (CSharp):
    1. IEnumerable SpawningMachin()
    2.     {
    3.         while(true)
    4.         {
    5.             yield return new WaitForSecondsRealtime(2);
    6.             Instantiate(Enemy, spawnSpots[randPos].position, Quaternion.identity);
    7.         }
    8.     }
    Try IEnumerable
     
  3. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Or Invoke.
    Code (CSharp):
    1. public float timeBeforeSpawn; // will spawn this many seconds after start
    2.  
    3. void Start ()
    4. {
    5.     Invoke ("SpawnThing", timeBeforeSpawn);
    6. }
    7.  
    8. private void SpawnThing ()
    9. {
    10.     Instantiate (Enemy, spawnSpots[randPos].position, Quaternion.identity);
    11. }
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Shouldn't the co-routine variant be:
    Code (CSharp):
    1. IEnumerable SpawningMachin()
    2.     {
    3.             yield return new WaitForSecondsRealtime(2);
    4.             Instantiate(Enemy, spawnSpots[randPos].position, Quaternion.identity);
    5.     }
    Else it will repeat, rather than be a one shot.