Time.timeScale ignored by some objects?

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 :slight_smile: ) 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.

Good luck!
Jeff.

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.

alongside setting Time.timeScale to 0.0 you could also use a global boolean like isGamePaused to check if the game is paused.

Try adding yield new WaitForFixedUpdate ();

To your loop before any other code, It should make it wait for the fixedupdate before it continues thus makeing the time.timescale to work on it.

Seems the wait for sec yield is messing it up since it doesnt care if physic time is paused or not.

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 :?

Try adding in a bool thats switched by a test key (that toggles on and off the animate) and see if that stops the animate on the instanced ones.

Just to see if its the loop or something else odd about it.

Cant see anything else with the script snippets.

Somewhat of a hack but you could put in a

while (time.timescale == 0)
{
yield
}

and that may stop it from runing wile the games paused.

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.

Thanks for the helpful suggestions.