En este momento estás viendo Cuando uso este script, bloquea mi juego.  ¿Qué estoy haciendo mal?

 – Unity

Cuando uso este script, bloquea mi juego. ¿Qué estoy haciendo mal? – Unity

Cuando uso este script, bloquea mi juego. ¿Qué estoy haciendo mal?

– UnityAssets3Free

hola , me llamo Daniel y para hoy os traigo
nueva pregunta

Quiero apagar las luces durante unos segundos después de que se active un evento, pero no funciona. Todavía soy muy nuevo en C#, así que si el código es feo, lo siento 🙁

[SerializeField] static public bool lightsOut;
private Light lightComponent;

void Start()

    lightComponent = gameObject.GetComponent<Light>();
    Time.timeScale = 0;


void Update()

    if (DeleteAfterFix.isFixed1)
    
        TurnOffLights();
    


IEnumerator WaitAndTurnOn()

    yield return new WaitForSeconds(2);
    TurnLightsOn();


void TurnOffLights()

    Destroy(lightComponent);
    WaitAndTurnOn();


void TurnLightsOn()

    gameObject.AddComponent<Light>(); 
    gameObject.GetComponent<Light>().type = 0;
    gameObject.GetComponent<Light>().intensity = 1 / 3;
    gameObject.GetComponent<Light>().range = 15 / 2;
    gameObject.GetComponent<Light>().spotAngle = 165;

2 respuestas 2

la llamada a

Time.timeScale = 0;

en el interior Start() función detiene el tiempo de juego, es decir, pausa el juego. Tira eso.

WaitAndTurnOn debe ser invocado como Coroutine:

StartCoroutine(WaitAndTurnOn());

Update la función no es necesaria si está utilizando Coroutines.

void Start()

    StartCoroutine(WaitAndTurnOn());


IEnumerator WaitAndTurnOn()

    yield return new WaitForSeconds(2);
    TurnLightsOn();


void TurnLightsOff()

    Destroy(GetComponent<Light>());
    StartCoroutine(WaitAndTurnOn());


void TurnLightsOn()

    gameObject.AddComponent<Light>(); 
    gameObject.GetComponent<Light>().type = 0;
    gameObject.GetComponent<Light>().intensity = 1 / 3;
    gameObject.GetComponent<Light>().range = 15 / 2;
    gameObject.GetComponent<Light>().spotAngle = 165;

Como ya se mencionó en esta respuesta, el principal problema es que no inicia su Coroutine como Coroutine a través de StartCoroutine.

Luego se mencionó, aunque un poco engañosamente: Tú estableces el

Time.timeScale = 0;

hacer el WaitForSeconds nunca terminará, incluso si lo ejecuta como Coroutine.

¿Puedes realmente mantener el

Time.timeScale = 0;

pero entonces tendrás que usar WaitForSecondsRealtime que ignora la escala de tiempo.

De hecho, puede ejecutar todo su código en una sola rutina usando

[SerializeField] private Light lightComponent;

// Yes, if you do this Unity automatically runs Start as a Coroutine!
private IEnumerator Start()

    if(!lightComponent)lightComponent = GetComponent<Light>();

    yield return new WaitUntil(() => DeleteAfterFix.isFixed1);

    var oroginalScale = Time.timeScale;
    Time.timeScale = 0;

    // Instead of destroy rather only disable the component but keep it intact
    lightComponent.enabled = false;
  
    // wait for 2 seconds ignoring the timeScale  
    yield return new WaitForSecondsRealtime(2f);

    // Simply enabled it again
    lightComponent.enabled = true;

    Time.timeScale = originalScale;

nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias

por hoy,espero que te funcione

Deja una respuesta