1
\$\begingroup\$

I want to pause 15 seconds then resume without any input when an object is collision other object this is my code but not working. Thank you

 void Start(){
    Eifftower = GameObject.FindGameObjectWithTag ("eiffeldespration");
    Eifftower.renderer.enabled = false;
    Time.timeScale = 1;
}

IEnumerator wait() {
    yield return new WaitForSeconds(15f);
    Eifftower.renderer.enabled = false;
    Time.timeScale = 1;
    Debug.Log("test");
}


void OnTriggerEnter2D(Collider2D collisionObject){
            //remove the game object
            if (collisionObject.tag == "Franch" ) {
            Eifftower.renderer.enabled = true;
            Time.timeScale = 1;
            StartCoroutine(test1());
            }
    }
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The WaitForSeconds() command does not run/return when Time.timeScale is equal to 0 because it relies on Time.timeScale. I'm assuming because you mention pausing, you set the Time.TimeScale to 0, then call "yield return new WaitForSeconds(15f)" and unpause with Time.timeScale = 1.

You can use Unity's Time.unscaledDeltaTime, which is how much real time has passed since the last frame.

Now in your code:

float waitCounter = 0;
IEnumerator wait() 
{
     waitCounter = 0;
     while (waitCounter < 15f)
     {
         waitCounter += Time.unscaledDeltaTime;

         //Yield until the next frame
         yield return null;
     }

     Eifftower.renderer.enabled = false;
     Time.timeScale = 1;
     Debug.Log("test");

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .