• 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
2
Question by raithza · Jun 24, 2013 at 02:52 PM · coroutinegameobject.active

How to not stop coroutines when deactivating a GameObject

Hi,

We pause our game by deactivating all gameobjects in the scene and then displaying a menu. This works great, except that any coroutines running on those gameobjects are stopped. Is there a workaround for this short of basically writing my own coroutine system?

Also: what is the logic behind stopping coroutines when a gameobject is deactivated? Why would you want to make such a drastic state alteration on an object if you only want to temporarily disable it?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Fattie · Mar 19, 2015 at 08:38 AM

The answer is just disable the script, not the whole game object.


For InvokeRepeating...

1) disable whole game object - InvokeRepeating does NOT stop

2) disable just the script component - InvokeRepeating does NOT stop

For coroutines...

3) disable whole game object - coroutine >>> DOES <<< stop

4) disable just the script component - coroutine does NOT stop

In all four cases, the repeat is "eliminated", it is NOT paused. If you again SetActive(true), in all four cases, the repeat does NOT continue where it left off.

Simply use OnDisble(), OnEnable() and the similar routines if you want custom behaviour on disabling.

Details here.

Comment
Add comment · 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
0

Answer by Sisso · Jun 24, 2013 at 04:31 PM

Coroutines serve the object and are dependent of object life cycle. If your coroutine need to run without your object, the courotine is in the wrong place.

By the way. Normaly, you use Time.timeScale = 0 to pause a game. Coroutine and yield WaitForSecond respect this value.

http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html

Comment
Add comment · Show 3 · 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 raithza · Jun 24, 2013 at 04:40 PM 1
Share

If your coroutine need to run without your object, the courotine is in the wrong place.

I'm not expecting that - rather that the coroutine gets suspended until I re-activate the object

By the way. Normaly, you use Time.timeScale = 0 to pause a game.

Our menu's etc have a lot of animated components with particle effects etc so we can't use that. We also want to pause the game for small intermissions while the player plays a $$anonymous$$igame.

avatar image Sisso · Jun 24, 2013 at 04:58 PM 0
Share

The classic way is use set timeScale to 0 and player effects and other times stuff using realtime. But I don't think that will be your case.

The main problem is that you are disabling your objects to simulate a "pause". Even if you solve it, you could have new problems in the future. Search some alternative to pause without disable. Like recursively stop all animations, effects and create a custom yield like "while (isShowing$$anonymous$$enu) yield return 0;"

Other option is use a coroutine schedule like you said. I think that this could help you "http://wiki.unity3d.com/index.php/CoroutineScheduler".

Last thing that I see is store the state of coroutines when stop, and start again when you re-enable your objects.

avatar image lordpaladin44 · May 18, 2016 at 06:40 PM 1
Share

By the way, as an experienced Unity app developer, setting Time.timescale = 0 is a TERRIBLE design decision. The design decision has caused numerous troubles down the road, and we are constantly having to work around it. The main reason is that when your paused, you still may want to do flashy stuff on the screen.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

[HELP] How to make camera smooth change position while follow object? 1 Answer

how do i use component gathered from ontriggerEnter, outside of that function? 1 Answer

2D dash in coroutine Inconsistent 1 Answer

UniRx or StartCoroutine in JobSystem? 1 Answer

How to get a value from a Coroutine? 1 Answer


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