I'm trying to make the coroutine spawn one object at a time every 0.2 seconds, but when I run the coroutine, it spawns multiple objects at once without any delay. How can I fix this and achieve the desired result?
My code:
if (shotExtender > 0)
{
StartCoroutine("Timer");
}
else
{
StopCoroutine("Timer");
}
Coroutine
IEnumerator Timer()
{
while (objCounter < 3)//it should spawn only 3 objects
{
yield return new WaitForSeconds(0.2f);
GameObject bullet = PhotonNetwork.Instantiate(bulletPref.name, bulletPosition, Quaternion.identity);
objCounter++;
Debug.Log("objCounter: " + objCounter + " maxBullets: "+maxBullets);
}
objCounter = 0;
shotExtender = 0;//This stops the coroutine
}