- Home /
Time.timeScale = 0 can't stop FixedUpdate()
I set Time.timeScale = 0 in FixedUpdate(). But, FixedUpdate() can't stop immediately. Excuse me, why can't I stop immediately? Thanks for any helpful answers.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test_TimeScale_5 : MonoBehaviour
{
private int ph_frame = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
ph_frame += 1;
Time.timeScale = 0;
Debug.Log("--FixedUpdate-- [ Time.timeScale=0 ] ----------- frame num: " + ph_frame.ToString());
}
}
Here is log picture:
Answer by badadam · Jan 17, 2019 at 01:57 PM
When you set Time.timeScale=0; animations stop but scripts continue to run. To stop FixedUpdate running, use static bool value as the script below.
public class Test_TimeScale_5 : MonoBehaviour {
private int ph_frame = 0;
public static bool isPause = false;
private void FixedUpdate()
{
if (isPause)
{
return;
}
ph_frame += 1;
Time.timeScale = 0;
Debug.Log("--FixedUpdate-- [ Time.timeScale=" + Time.timeScale.ToString() + "], frame num: " + ph_frame.ToString());
}
}
And when you want to pause the script from another script, use this code below.
Test_TimeScale_5.isPause = true;
Time.timeScale = 0;
Scripts stop too, fixedupdate and update stop being called the issue is not being stopped at the exact moment, you stop time.timescale
Simply talk about our game: There is only one player in the game, to collide with other gameObjects. For example: Palyer, cube_1, cube_2, cube_3, cube_4... cube_N. When the player click on the pause, I can stop the movement immediately with this script. But other gameObjects, like cube_1, cube_N, they are automatically moved by the physics engine, not by script. And we hope that they can stop accurately and immediately. Because this is a networked game based on frame synchronization. Do you have any suggestions? Thank you.
Answer by xxmariofer · Jan 17, 2019 at 09:23 AM
There must be something else setting time.timescale to something else than 0, i was testing by changing FixedUpdate frequence in case it has to wait for the next update to change the time.scale value but even changing fixedupdate frequence to 1 or to 0.000001(ended up crashing) the time.scale stop was inmidiate, can you please log Time.timeScale value in the fixed update just for making sure is 0?
Answer by ccgtest · Jan 17, 2019 at 12:24 PM
Thank you for your help, but the problem is still not solved. This is new code. From the log, I think Time.timeScale value is 0 at runtime. But, FixedUpdate() can't stop immediately. We are make a physics game, and we hope to stop FixedUpdate() immediately. Sorry, my English is not fluent.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test_TimeScale_5 : MonoBehaviour
{
private int ph_frame = 0;
void FixedUpdate()
{
ph_frame += 1;
Time.timeScale = 0;
Debug.Log("--FixedUpdate-- [ Time.timeScale=" + Time.timeScale.ToString()+ "], frame num: " + ph_frame.ToString());
}
}
the easiest way of fixing it would be with an if statement at the beginning of the fixedupdate checking if time.timescale == 0; searching in the web i saw you can use Physics.Simulate for managing the physics yourself that might work.