I am making a 2d game in Unity and where I am instantiating enemy Using This Code
void Update()
{
StartCoroutine("EnemyInstance");
}
IEnumerator EnemyInstance()
{
float positionRandoming = Random.Range(1f, 2f);
if (positionRandoming < 1.5f)
{
Instantiate(enemyPrefeb, new Vector3(-4.3f, -1.45f, 1f), position1.rotation, transform.parent);
enemyScript.pos = 1;
}
if (positionRandoming >= 1.5f)
{
Instantiate(enemyPrefeb, new Vector3(3.6f, -1.45f, 1f), position2.rotation, transform.parent);
enemyScript.pos = 2;
}
yield return new WaitForSeconds(2.4f);
}
In this code the IEnumerator method is doing their work but not yield return new WaitForSeconds. Means that if I run it in Unity the enemy is instantiating in every frame. How can I solve it?