- Home /
Delaying Time.timeScale =1 when unpausing (to allow time for animation to clear)
I know there are a LOT of time related questions on this board, and I swear I've read every last one of them. I've been searching and trying things for 3 hours now with no luck. With that said, here's my problem.
When I pause the game, I set Time.time = 0, everything is gravy. When I resume, the animation for my pause menu takes just a split second to clear, but this is a fast paced game so I want time.timeScale to still = 0 for a half second after unpausing in order for the animation to clear. Here's what I have so far that ALMOST works:
public void ReseumeGame() {
StartCoroutine (Resuming());
unPauseSound.Play ();
pauseMusic.Stop ();
backgroundMusic.UnPause ();
}
IEnumerator Resuming(){
float pauseTime = Time.realtimeSinceStartup + 3f;
Debug.Log ("Before wait.");
while (Time.realtimeSinceStartup < pauseTime)
{
yield return 0;
}
Debug.Log ("After wait."); // Waits 3 seconds like it should.
Time.timeScale = 1; // Executes immediately without waiting!!! >_<
pauseButton.SetActive (true); // Waits 3 seconds like it should.
}
I'm using 3 seconds here just to make the delay obvious. As it stands right now, upon unpausing, before wait is displayed, Time.timeScale = 1 executes, 3 seconds passes, after wait displays, and the pause button is set active again... meaning everything is delayed like I want it to be EXCEPT FOR TIME.TIMESCALE=1. Is that function somehow immune to delays? I'm not sure how the Debug.Log("After wait.") and pauseButton.SetActive(true); that is before and after Time.timeScale = 1 can be delayed without the Time.timeScale = 1 being delayed as well.
Any help would be immensely appreciated, I'm losing hairs over this.
Has anyone else ever run into this before? I can't be the only one who has tried delaying this... :P
Answer by Olgo · Apr 14, 2015 at 11:57 PM
I dropped your code onto a gameobject in one of my scenes and it works perfectly. I had to make a couple adjustments because I dont have music playing components set up. but it works exactly as it should as follows.
I'm not sure what your pauseButton.setActive(true) is about. I commented that out because I dont have the supporting code. It seemed weird though because on resuming, wouldn't you want your pauseButton.setActive to be false? Just seems counter intuitive. maybe theres an issue there?
However the time scaling works perfectly.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
void Start(){
Time.timeScale = 0;
}
void Update(){
if (Input.GetKeyUp (KeyCode.Space)) {
ReseumeGame();
}
}
public void ReseumeGame() {
StartCoroutine (Resuming());
//unPauseSound.Play ();
//pauseMusic.Stop ();
//backgroundMusic.UnPause ();
}
IEnumerator Resuming(){
float pauseTime = Time.realtimeSinceStartup + 3f;
Debug.Log ("Before wait.");
while (Time.realtimeSinceStartup < pauseTime)
{
yield return 0;
}
Debug.Log ("After wait."); // Waits 3 seconds like it should.
Time.timeScale = 1; // Executes immediately without waiting!!! >_<
//pauseButton.SetActive (true); // Waits 3 seconds like it should.
}
}
After quitting in frustration last night... I just tried it again and you're right, now it works >_< I have no idea why it wasn't working last night. $$anonymous$$ust have been a strange Unity bug (though I even tried restarting Unity last night to see if it was a bug). $$anonymous$$aybe the update somehow helped? Lol, I dunno. Very strange.
Thanks for trying it out though!