- Home /
 
Having multiple objects fire prefabs in different times C#
I'm trying to have multiple objects fire from the same prefab but its not working. I created an array to hold the prefabs and using a coroutine to initiate it. Any help would be much appreciated thank you.
The code looks like this:
Adding the Prefab Variables
 public GameObject[] bossWeapons;
 //Prefab to instantiate
 public GameObject EnemyAmmo_Prefab;
 //Speed of ammo
 public int AmmoSpeed = 1000;
 
               Setting up the Coroutine
  IEnumerator bossWeaponFire()
     {           
         yield return new WaitForSeconds(2.0f);
 
         for (int i = 0; i < bossWeapons.Length; i++ )
         {
             bossWeapons[i] = Instantiate(EnemyAmmo_Prefab, GameObject.Find("Enemy_Ammo_spawnPoint").transform.position, Quaternion.identity) as GameObject;
 
             //Uses rigidbody to move the bullet
             bossWeapons[i].rigidbody.AddForce(Vector3.down * AmmoSpeed);
 
             yield return null;
         }
     }
 
               Running the Coroutine
  void Start()
     {
         StartCoroutine(bossWeaponFire());
     }
 
              
               Comment
              
 
               
              What does "not working" mean? You have to set the length of bossWeapons array to some value in the Inspector before anything will be created. Another potential problem is that, with only one frame between the creation of each game object, there is a chance the objects will collide. Consider replacing 'yield return null;' with 'yield return new WaitForSeconds(0.1f);'
Your answer