Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Unity: no se pueden crear varios objetos con un retraso entre la creación

Estoy tratando de hacer que la corrutina genere un objeto a la vez cada 0,2 segundos, pero cuando ejecuto la corrutina, genera varios objetos a la vez sin demora. ¿Cómo puedo solucionar esto y lograr el resultado deseado?

Mi código:

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

Corrutina

 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 Respuestas
Responde la pregunta

0

Intente sacar el reiniciador del contador de objetos y el reiniciador del extensor de tiro fuera de la rutina. ¡Creo que se accede a la declaración if varias veces generando múltiples rutinas antes de que la primera haya terminado!

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

0

Si este código se ejecuta dentro de la actualización

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

Debe finalizar la condición antes de iniciar el proceso de esta manera

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

0

Las respuestas anteriores me pusieron en el camino correcto. Hice todo de la siguiente manera y funcionó:

 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda