Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by mchts · Mar 30, 2016 at 07:12 AM · script.updatepauseenabledpause game

Enable/disable is not working as expected

Hi everyone,

To handle pausing i created a base class called Stoppables and add a listener to it which listens the pause event. When game is paused i set this.enabled = false; in Stoppable script to ensure that each script derived form Stoppables stop calling update. I'm not sure if this is the best approach but i choose this over creating an interface for stoppable objects.

The problem is this works for two out of three scripts which of all are attached to the same gameobject and at the same level of hierarchy. I'm sure that all these three scripts are derived from Stoppables. Am i missing something?

Any suggestions will be appreciated. Thanks in advence...

Comment
Add comment · Show 2
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 Fredex8 · Mar 30, 2016 at 08:48 AM 0
Share

$$anonymous$$ight be easier for you to just use Time.timeScale = 0; to pause and Time.timeScale = 1; to unpause rather than trying to do this. Enabling and disabling scripts is going to cause you more problems than it is worth just to pause the game.

Time.timeScale = 0; tends to work find for most things but it can cause some problems with Coroutines so you'd only need to put in separate conditions for objects using those.

avatar image mchts · Mar 30, 2016 at 12:11 PM 0
Share

Thanks for fast reply. I already use timescale but it only effects animations in my case. I had hoped timescale would be enough to pause all game but unfortunately update functions still keep running, so i went with the solution i told above. Seems enable/disable is not working properly at all. So any other suggestions for pause? I galdly take advices.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Mar 30, 2016 at 03:49 PM

You can say when time is = 0 player.SetActive(false); when 1 it's set to true

Comment
Add comment · Show 1 · 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 mchts · Mar 30, 2016 at 04:28 PM 0
Share

Yes but SetActive(false) disables whole gameObject which means disappearance of objects in the scene. I just want them to stop. Looks like boolean check in update function is the only solution for now. But isn't there any other solution beside it??!!

avatar image
0

Answer by NoseKills · Mar 30, 2016 at 04:43 PM

When game is paused i set this.enabled = false; in Stoppable script to ensure that each script derived form Stoppables stop calling update.

You can't use inheritance to do something like this. I mean even if it did work like that, the property "enabled" is already inherited from the Behaviour class, so changing its value would apply to all (Mono)Behaviours and stop all your scripts.

You should just use a static variable or such. Or use GetObjectsOfType() and set enabled to all of them.

Inheritance is just a way to organise your code and to make a system where you don't have to copy/rewrite the common code when making classes that have a lot in common. Or to enable putting different classes into the same typed collection. Each instance of each subclass is still its own... instance, and all its variables are its own. The this keyword also always refers to just 1 instance.

Comment
Add comment · Show 2 · 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 mchts · Mar 30, 2016 at 05:26 PM 0
Share

The idea behind what i do is using delegates and events. But I just didn't want to implement a listener method for each stoppable object (this is where interface steps in) since the number of stoppable objects may increase in time. If i get to what you said, i already put Stoppables into a List and make a for loop to enable them when game resumes as you mentioned. So following the same way when disabling them seems like the best case. Thank you all for your time..

avatar image mchts · Mar 30, 2016 at 06:35 PM 0
Share

Doing so made the trick. Thank you all again...

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.

Follow this Question

Answers Answers and Comments

54 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 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 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

Instantiate only once in Update and be able to use the clone variable in a switch statement(in Update). 1 Answer

Update() and FixedUpdate() not running on C# scripts 0 Answers

How do I detect if an Admob Banner Ad has been tapped/opened? 1 Answer

Input System no button press when timescale = 0 1 Answer

Double press script 2 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