- Home /
wait 3 seconds then resume c#
hello ...
am trying to pause my game using time.timescale=0 ... but when i want to resume my game after 3 seconds it doesn't work as i followed this solution: http://answers.unity3d.com/questions/9878/freeze-game-using-timetimescale-0-wait-3-secs-and.html
and here is my code :
private IEnumerator Pause(int p)
{
Time.timeScale = 0.1f;
float pauseEndTime = Time.realtimeSinceStartup + 1;
while (Time.realtimeSinceStartup < pauseEndTime)
{
yield return 0;
}
Time.timeScale = 1;
}
void ButtonUp()
{
StartCoroutine( Pause() );
guiTexture.texture = ResumeBtn;
PauseButton.guiTexture.texture = PauseBtn;
PauseMenu.gameObject.SetActiveRecursively(false);
}
it resumes the game but with time.timescale = 0.1f only ... any ideas
It should work. Try to yield null ins$$anonymous$$d of 0, and print Time.realtimeSinceStartup to check if it increases.
"it resumes the game but with time.timescale = 0.1"
What do you mean by "resumes"? When you click, the time scale should snap to 0.1 (is this a test value, which will become 0 when it's working?) If it never goes back to 1, I think it is NOT resu$$anonymous$$g.
FakeBerenger you gave me the solution thanks :)
Owen Reynolds i mean it the script is stopping on the line where Time.timscale = 0.1f; but i solved it :) thanks for your time
Answer by Rayan Adam · Nov 13, 2012 at 05:55 PM
I already solved it thanks :
void Awake(){ first = 0; second = 0; third = 0; resumeIn3Seconds = false; }
void Update()
{
ResumeIn3Seconds();
}
void ResumeIn3Seconds(){
if(resumeIn3Seconds){
Counter123.active = true;
StartCoroutine(ResumeAfterSeconds(3));
}
}
private IEnumerator ResumeAfterSeconds(int resumetime) // 3
{
Time.timeScale = 0.0001f;
float pauseEndTime = Time.realtimeSinceStartup + resumetime; // 10 + 4 = 13
float number3 = Time.realtimeSinceStartup + 1; // 10 + 1 = 11
float number2 = Time.realtimeSinceStartup + 2; // 10 + 2 = 12
float number1 = Time.realtimeSinceStartup + 3; // 10 + 3 = 13
while (Time.realtimeSinceStartup < pauseEndTime) // 10 < 13
{
if(Time.realtimeSinceStartup <= number3) // 10 < 11
Counter123.guiTexture.texture = Three;
else if(Time.realtimeSinceStartup <= number2) // 11 < 12
Counter123.guiTexture.texture = Two;
else if(Time.realtimeSinceStartup <= number1) // 12 < 13
Counter123.guiTexture.texture = One;
yield return null;
}
Counter123.active = false;
resumeIn3Seconds = false;
Time.timeScale = 1;
}
Answer by Montraydavis · Nov 13, 2012 at 02:06 PM
yield WaitForSeconds ( 3 ) ; // Just don't add it to Update. Use a co-routine.
Answer by Karsnen_2 · Nov 13, 2012 at 05:06 PM
Can't you just pause the game and then Invoke a function to change the TimeScale value, 3 seconds later.
Something like this.
void Update ()
{
// When you need to
pauseGame ();
Invoke("pauseGame",3.0f);
}
void pauseGame ()
{
if(Time.timeScale == 1f)
Time.timeScale = 0f;
else Time.timeScale = 1f;
}
Unfortunately, this uses the timescale to call the Invoke method as well, so no, it won't work :)
Your answer
Follow this Question
Related Questions
Time.timeScale doesnt work with GUI? 0 Answers
Can't set Time.timeScale back to 1. 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GUI resume button not working? 4 Answers