• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unit_power · Dec 29, 2013 at 08:20 AM · ontriggerenterpausetimescale

Pause OnTriggerEnter

I dont understand why this doesn't work. All i am trying to do is pause the game for one sec OnTriggerEnter:

 using UnityEngine;
 using System.Collections;
 
 public class LevelInfo : MonoBehaviour {
 
     public bool pause;
 
     void Update()
     {
         if (pause){
             Time.timeScale = 0;
         }
         if (!pause){
             Time.timeScale = 1;
         }
     }
     
 
     void OnTriggerEnter (Collider col){
         if (col.gameObject.tag == "Player"){
             StartCoroutine(pauseTime());
             print ("Pause in action");
         }
     }
     
     
     IEnumerator pauseTime(){
         pause = true;
         yield return new WaitForSeconds(1f);
         pause = false;
     }
 
 }


Thank you

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by KellyThomas · Dec 29, 2013 at 08:25 AM

WaitForSeconds runs at Time.timeScale.

As you set timeScale to 0 it will never stop waiting.

More detail and a couple of solutions can be found here.

Here is an implementation that monitors Time.realtimeSinceStartup:

 using UnityEngine;
 using System.Collections;
 
 public class Pause : MonoBehaviour
 {
     private float pauseUntil;
     private bool paused;
 
     void Update() {
         if (paused) CheckPause();
     }
 
     void OnTriggerEnter(Collider col) {
         if (col.gameObject.tag == "Player") {
             PauseForDuration(1.0f);
         }
     }
 
     void PauseForDuration(float duration) {
         paused = true;
         pauseUntil = Time.realtimeSinceStartup + duration;
         Time.timeScale = 0.0f;
     }
     
     void CheckPause() {
         if (Time.realtimeSinceStartup >= pauseUntil) {
             Time.timeScale = 1.0f;
             paused=false;
         }
     }
 }
 
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unit_power · Dec 29, 2013 at 09:05 AM 0
Share

That works by itself, but once i put it inside the OntriggerEnter it doesn't seem to work.

avatar image KellyThomas · Dec 29, 2013 at 09:25 AM 0
Share

I think your code currently has the following behaviour:

In pauseTime():

  • set pause to true

  • start waiting for 1 sec

Then in the next Update():

  • set timeScale to 0

As a result one second of game time will never pass.

Two solutions are available at the question I linked to above.

Please try one of those and tell me how it goes.

avatar image unit_power · Dec 29, 2013 at 09:02 PM 0
Share

The TimeScale make sense Thank you $$anonymous$$elly but that doest explain why this does not work:

 using UnityEngine;
 using System.Collections;
 
 public class LevelInfo : $$anonymous$$onoBehaviour {
 
     void OnTriggerEnter (Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             Time.timeScale = 0.0000001f;
         }
     }
 }
 
avatar image KellyThomas · Dec 30, 2013 at 02:07 AM 0
Share

Are you then calling:

 yield return new WaitForSeconds(0.0000001f);
avatar image unit_power · Dec 30, 2013 at 02:39 AM 0
Share

In this case all i want to do is Pause on trigger Enter. But that is the part that doesn't work.

So on: OnTriggerEnter = Time.timeScale =0 (Doesn't run the timescale)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to enable UI collision while timeScale is 0? 2 Answers

Pause menu... Isn't pausing everything... 1 Answer

Timescale wont work 0 Answers

How to Appease a pause menu in a game that changes it's time scale? 1 Answer

Animations ignore TimeScale 8 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges