- Home /
How do I pause ONLY c# script but not objects/effects?
Objective 1: speed. No instantiates/destroys (memory management/garbage collection is slow!)
Objective 2: particle effects continue to play, and in-place.
Objective 3: after effect/pause has ended, continue to run script.
Scenario: Object has attached particle effect that is tripped by event, and event results in deactivation of this.object's rendering and eventual relocation of this.object. Desire is for the effect to be seen in full at this.object's current location before it is moved. However, the only way I've found for this to occur is to use Destroy(object) with a timed delay; moving this.object causes the effect to move with it, even though "global" space has been checked in the particle effect options. The object then must be Instantiated from the prefab (with attached effect disabled) upon next use. With over 500 of the same object being constantly Destroyed and Instantiated, cleanup slows things down considerably! But my attempts at recycling the objects through simply {renderer.enabled=false; pause(){yield WaitForSeconds(x);} transform.position} have hit {this.snag} !LOL with the particle system... any ideas? :)
Rather than Destroy(), couldn't you just turn the renderer off on the object then move and re-enable the renderer when the effect is done? Or alternately, could you put the particle effect on a child object, and either change the local position of the child object to keep it at the same position when the parent moves setting the localPosition to (0,0,0) when the effect is done, or breaking the parent/child relationship during the playing of the effect and then reestablishing it after playing?
GameObject.Find("LevelNo").GetComponent<scriptname>().enabled=false;
Thank you all for your answers, I appreciate the time taken! Perhaps a little "fill-in" would be helpful... Each of my objects has a lattice-work of scripts and objects that are inter-dependent, one reason the frames dropped when I programmed quick-and-dirty (instantiate/non-render/destroy) as a proof-of-concept. I now know the system works (slowly) and am cleaning up the code to win frames back.
Creating children of all the parents/children (~10 children for each parent, some of which circumstantially create/recycle additional parent objects which create/recycle children of their own - the circle of life) would leave me in much the same situation again: now a slew of additional objects - an entire second set - for the code to manage and chew upon while the system slows. This may change the symptom's root cause but would maintain the result.
Enabling and disabling scripts through the addition/creation of additional scripts to manage the first set of scripts (which already activate/deactivate each other) lacks the "elegance and $$anonymous$$imization of code" towards which we all strive, with much the same management challenges as creating more objects - albeit more to the cost of the coder(s) to manage rather than the system itself... and would leave the symptom's true cause intact.
As per "elegance", both of these seem a work-around to the root problem. I hesitate to build one Rube Goldberg machine as a means of cleaning out another. And neither one will have taught me additional skill, something we all seek during our travails in the Game of Code :)
The root question is this: can the particle system itself be rooted to a specific global location until it completes it's playback, or as an alternative can it be coerced into continuous play while the script attached to that same object (and plays said effect) is paused before translating the object? The particle system is set to burst particles instantly and in global, not emit them over time nor in local space; why do they follow the object after pausing (?!) with the script? Why/how is the scripting system linked to the particle system in this way, I had thought them to be independent (though not fully multithreaded) streams? It seems equivalent to the pausing of some script attached to an object also causing that object's colliders to suspend operation. A satirical observation, perhaps; I find myself confused and bewildered upon discovering this see$$anonymous$$g inconsistency...
Hmmm... as solutions seem scarce, I've worked around the problem (though not eli$$anonymous$$ated it) by having an v.Update check for ParticleSystem.isPlaying. Still using processor I'd rather devote to something else, and had to create a "buffer" of objects to compensate for realistically inactive objects in play during the wait cycle... but it's functional. Thanks all for your views and thoughts :)
Answer by SnotE101 · Sep 18, 2014 at 05:08 PM
Try
bool disableScript;
public scriptYouWantToStop scriptYouWantToStop;
void Update
{
if(disableScript)
{
scriptYouWantToStop.enabled = false;
}else {
scriptYouWantToStop.enabled = true;
}
}`
This is untested code, just drag the script you want to stop into the "scriptYouWantToStop" variable in the inspector.
That is an example of how to pause a script. I think you should follow robertbu's advice though and disable the mesh renderer.
Your answer
Follow this Question
Related Questions
Enabling Particle System's Emission Results Huge Amount Of GC 0 Answers
How to pause emitter in new Particle System? 2 Answers
How do I pause and unpause multi gameobjects in the scene for a five seconds 1 Answer
Pause the video in unity after playing it but it will done automatically after some time of period. 0 Answers
How to Pause game 1 Answer