I have a coroutine that smoothly changes a variable in my UI, but when I pause the game, the coroutine pauses too. How can I fix that?
Code:
using UnityEngine;
using System.Collections;
public class HUDController : MonoBehaviour {
public void PauseGame() {
FreezeTime();
StartCoroutine(ShowPauseMenu());
}
private void FreezeTime() {
Time.timeScale = 0;
}
private IEnumerator ShowPauseMenu() {
var pauseMenuCanvasGroup = GameObject.FindWithTag("PauseMenu").GetComponent<CanvasGroup>();
var alphaValue = 1f;
var velocity = 0f;
var time = 0.15f;
while (!Mathf.Approximately(pauseMenuCanvasGroup.alpha, alphaValue)) {
pauseMenuCanvasGroup.alpha = Mathf.SmoothDamp(pauseMenuCanvasGroup.alpha, alphaValue, ref velocity, time);
yield return null;
}
pauseMenuCanvasGroup.alpha = 1; // Since float is not accurate, manually set the alpha to 1
pauseMenuCanvasGroup.interactable = true;
}
}
yield return null
ornew WaitForEndOfFrame()
keep humming along just fine whenTime.timeScale = 0
- it's onlyyield return new WaitForSeconds(delay)
that pause. Can you confirm what version of Unity you're running? \$\endgroup\$