This question was
closed Oct 05, 2018 at 08:18 PM by
Vicarian for the following reason:
OP didn't close, indicated resolution.
Question by
RajOTS · Oct 05, 2018 at 06:33 PM ·
c#gameobjecttagscounterspawning problems
Spawning a number of enemies while condition is true
I've been stuck on this problem for awhile, I'm trying to spawn a certain number of "Enemies" only if the number is equal or below the variable I set. However no matter what I put it only spawns one or none at all. Please help if you can!
public GameObject Enemy;
int totalNumberEnemy = 2;
int numberEnemy;
bool canSpawn;
void Start()
{
canSpawn = true;
StartCoroutine(EnemySpawn());
}
void FixedUpdate()
{
numberEnemy = GameObject.FindGameObjectsWithTag("Enemy").Length;
print(numberEnemy);
if (numberEnemy < totalNumberEnemy)
{
canSpawn = false;
}
}
IEnumerator EnemySpawn()
{
while(canSpawn)
{
Instantiate(Enemy, transform.position, Quaternion.identity);
yield return new WaitForSeconds(2f);
}
}
Comment
So, your enemies spawn enemies ? You should watch a tutorial on "Object Pooling".
Never-$$anonymous$$d I figured it out just used a while loop for my EnemySpawn and added "numberofEenemy <= TotalNumberEnemy"
Follow this Question
Related Questions
Spawning stops after second scene load 2 Answers
list.count gives back odd numbers c# 1 Answer
How to correctly count number of objects? 2 Answers