Limiting the number of clone objects

Hi!!

How do I set the number of objects in the enemy clone?

thanks :slight_smile:

Code this:

using UnityEngine;
using System.Collections;

public class EnemyStart : MonoBehaviour {
	
	
	public Transform enemy;
	

void Update () {
		
		
		enemy.transform.position = new Vector3(Random.Range(-12.0f,10.0f),0,0);
		
		Instantiate(enemy,enemy.position,transform.rotation);

	}
}

I would just use a for loop :wink:

Hi

The first idea that comes to mind would be to wrap the Instatiate code in an if-statement and create an enemyCout variable where you preset how many enemies you want, then check the count in the if-satement to see if the count is less than or equal to the value in enemyCount.

Whether this would be the correct way of doing it, I don’t know, but as I wrote earlier this is the first idea that come to my mind.

I believe that a loop would be the easiest way actually. Have it check if the number of clones = to max number of mobs, if it is not, instantiate some more :wink:

thanks friends!! :stuck_out_tongue: happening with the for loop…

public Transform enemy; 
public int enemyCount = 100;
	
	void Start(){
		
		StartCoroutine(method());

	}

	
	void Update () {


    }
	
	IEnumerator method(){
		
	   for(int a = 0; a < enemyCount; a++){
		
		   yield return new WaitForSeconds(1);

	       Instantiate(enemy,enemy.position,transform.rotation);
			
		   enemy.transform.position = new Vector3(Random.Range(-12.0f,10.0f),0,0);

		}
			
		   
	}

Good thing that its working. Good luck with the game.

-Jonas Rasmussen