Hi, I have a slight problem which for the life of me I can’t figure out why it’s happening…
I have some simple pause code in my game manager script that sets the Time.timeScale = 0 to pause the game and i’m using some buttons in the OnGUI to set it back to 1 when the player wants to resume, this code works fine except that some of the objects in the scene seem to ignore this.
I’m using a simple for loop to animate textures in an array (thanks to eric for helping me with that ) code is as follows…
var waitTime = 1.0/animationSpeed;
for (i = 0; i < texturesForward.Length; i++)
{
renderer.material.mainTexture = texturesForward[i];
yield WaitForSeconds(waitTime);
}
this code is part of a function and for some odd reason seems to ignore the Time.timeScale and continues to run through the loop when Time.timeScale is set to 0…
I also use the same code to run a countdown timer from the start of the level :
var countdownTimer = WaitTime;
for (i = 0; i < WaitTime; i++)
{
timerTextObject.text = "Time \n \n" + countdownTimer;
countdownTimer--;
if (countdownTimer == 45)
{
audio.PlayOneShot (timerDing);
}
else if (countdownTimer == 30)
{
audio.PlayOneShot (timerDing);
}
else if (countdownTimer == 15)
{
audio.PlayOneShot (timerDing);
}
yield WaitForSeconds(1);
}
As you can see it’s basiclly the same for loop and this loop does stop and resume when the game is paused and Time.timeScale = 0.
The countdown timer code resides in the game manager script for each scene and the texture animating code is attached to the prefabs which are instaniated during the scene from player clicks.
So, i’m now at a loss as to why one for loop (the timer) stops / resumes when the timescale is set to 0 and the other one (texture animator) just continues and seems to ignore the timescale. :?
Anything in FixedUpdate will be affected by it, but scripts running in ‘Update’ or ‘LateUpdate’ will continue to run regardless of the timescale since they are not time dependent functions.
Check to see whether those things that are being ignored are within ‘FixedUpdate’ or one of the others.
sadly not, the only script in the level using the Update function is the game manager and it only uses this function to check if the player presses escape to pause the game by setting the timescale to 0.
Sadly, it didn’t work, the objects continue to animate and so on, after a little more checking I have also noticed that it seems to be only the instantiated objects that are not working as i’m using the exact same script on an object that’s allways in the scene from the start and that objects texture animation stops and resumes with no problem :?
This issue is now fixed via some creative recoding of the functions and I added a new function to resume the animation from the last frame when the game is unpaused.