- Home /
 
 
               Question by 
               Giakaama · Mar 29, 2014 at 03:15 PM · 
                randomenemiesrandomspawningspawner  
              
 
              Enemy Spawner based on enemies values !
Hi, maybe someone can help. I've got a spawner that I want to spawn monsters based on dificulty ( Values from 1 to 10).
I'm putting the enemies in arrays then I want to check if a value is equal to 1 to 10 then spawn the enemies from the specified array and subtract the value from the Budget.
The problem is that I really don't know how to do a random.range through all the arrays and instantiate the enemies from all of them.
The end result I want is that if I got a budget of 10, because of the random.range i want sometime to spawn 2 enemies of value 5, or 10 enemies of value 1 or enemies of values that sum up 10.
Or maybe is another way to do this ? :)
Thank you in advance
Here is the code I've got:
     using UnityEngine;
     using System.Collections;
     
     public class GameControllerTEST : MonoBehaviour {
     
     
         public GameObject[] easyEnemies;
         public GameObject[] intermediateEnemies;
         public GameObject[] mediumEnemies;
         public GameObject[] advancedEnemies;
         public GameObject[] hardEnemies;
         public GameObject[] bossEnemies;
          private int v1, v2 ,v3 , v4,v5 ,v6 ,v7 ,v8 , v9 ;
     
     
         public int budget = 10;
     
         public Transform playerPosition;
        
         public float spawnWait;
         public float startWait;
         public float waveWait;
     
     public int waveCounter = 0;
     
         private float distance = 30f;
        
         private GameObject enemySpawn;
         
     //Start 
     
         void Start()
         {
           
             StartCoroutine(SpawnWaves());
             
             enemySpawn = new GameObject();
         }
                 
         
     // Spawning Waves
     
         IEnumerator SpawnWaves()
         {
     
             yield return new WaitForSeconds(startWait);
             while(true){
     
                 SpawnWaves();
                 yield return new WaitForSeconds(spawnWait);
                 
                 yield return new WaitForSeconds(waveWait);              
             }
         }
 
 // Spawn Enemies
           
         private void SpawnEnemy()
         {
             //easy enemies
             if (v1 == 1) 
             {
     
                     SpawnPosition(easyEnemies);
                     budget--;
                 
             }
     
             if (v2 == 2) {
                 SpawnPosition(intermediateEnemies);
                 budget -= 2;
             }
     
         
         }
 
 // Instantiate Enemies
         private void SpawnPosition( GameObject[] enemyarray)
         {
             Vector3 spawnPosition = new Vector3(Random.Range(-playerPosition.transform.position.x-4f, playerPosition.transform.position.x+2f), playerPosition.transform.position.y, playerPosition.transform.position.z+distance);
             Quaternion spawnRotation = Quaternion.identity;
             GameObject toSpawn = enemyarray[Random.Range(0,enemyarray.Length)];
             GameObject enemy = (GameObject) Instantiate(toSpawn,spawnPosition,spawnRotation);
         } 
     
     
     
     }
     
 
              
               Comment
              
 
               
              Your answer