Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
Unity - Can't create multiple objects with a delay between creation

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
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try taking out the object counter resetter and the shot extender resetter outside the coroutine. I believe that the if statement is being accessed multiple times generating multiple coroutines before the first one has even finished!

   if (shotExtender > 0)
{
    StartCoroutine("Timer");
    objCounter = 0;
    shotExtender = 0;//This stops the coroutine
}
over 4 years ago · Santiago Trujillo Relatório

0

If this code runs inside the update

if (shotExtender > 0)
{
   StartCoroutine("Timer");
}
else
{
   StopCoroutine("Timer");
}

You must finish the condition before starting the process like this

if (shotExtender > 0)
{
  shotExtender = 0;
   StartCoroutine("Timer");
}
else
{
   StopCoroutine("Timer");
}
over 4 years ago · Santiago Trujillo Relatório

0

The answers above got me on the right track. I did everything as follows and it worked:

if (shotExtender > 0)
{
    StartCoroutine("Timer");
}

.

    IEnumerator Timer()
{
    while (objCounter < maxBullets)
    {
        yield return new WaitForSeconds(0.2f);
        GameObject bullet = PhotonNetwork.Instantiate(bulletPref.name, bulletPosition, Quaternion.identity);

        shotCounter++;
        Debug.Log("corutine works. objCounter: " + objCounter + " maxBullets: "+maxBullets);
    }
    StopCoroutine("ShotTimer");
    objCounter = 0;shotExtender = 0;


}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda