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...
$$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.
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.
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
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??!!
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.
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..