- Home /
Why does script continue executing when turned off or destroyed?
Started working on a script i plan to publish, in doing so i am making LogWarnings and such for end users. I came across this, not so much problem, but undesired effect and am curious as to how the unity pipeline runs/interprets the code.
I have a check to make sure that an array(builtin) is populated, if there is nothing there i destroy the script(turning it off isnt fast enough, surprisingly). The 'issue' arises right after i destroy the script, since i make a function call right after the check the script continues to run for 1 more line(thus throwing a null since the array is not populated, even tho the script was 'destroyed' right before(i want the user to only see my Log Warning(not the un-descriptive null ref we all hate getting :P ). I have managed to get around it by using Destroy(this); followed by a yield;
Im curious though:
Why does it continue running?
How, when i use this.enabled=false; followed by yield;, is it still able to run the function and first line inside?
Heres some sample code for a quick read example (length is declared as waypoints.Length)
Runs first line of function Foo (BAD)
else if(length==0)
{
Debug.LogWarning("Assign waypoints to the waypoints array");
this.enabled=false; //or //Destroy(this);
}
Foo();
Runs first line of function Foo (BAD)
else if(length==0)
{
Debug.LogWarning("Assign waypoints to the waypoints array");
this.enabled=false;
yield;
}
Foo();
Works (does not run function Foo) (GOOD)
else if(length==0)
{
Debug.LogWarning("Assign waypoints to the waypoints array");
Destroy(this);
yield;
}
Foo();
Again mainly just curious how the script executes(this is a very small amount of time here, ~ 1 frame). If anyone wishes to explain id be grateful, even tho it isnt of huge concern it may help others(and myself for that matter) understand the engine a bit better.
Answer by TomasRiker · Jun 23, 2012 at 07:26 AM
Script execution doesn't stop immediately when you disable or destroy an object. That's perfectly normal. If you want to exit your current function, use return
.
You know, since i have the call in start i never even thought to say return; .... but it worked ever so elegantly. I suppose it is the simple things in code that make it so good! Thats what i needed. Thanks
Answer by SarperS · Jun 23, 2012 at 06:51 AM
As Unity scripting manual states "Enabled Behaviours are Updated, disabled Behaviours are not." That doesn't stop you from calling specific methods from them though.
You can try DestroyImmediate as Destroy method is a delayed method and maybe that's why it appears to be slow.
I understand completely what you are saying, and for a second had a realization "Oh yeah, destroy immediate doi!" but i get a different execption, a more unlikely one, "$$anonymous$$issing Reference Exception, object has been destroyed but you are still trying to access it". I seriously thot you had it to a 't' there, but i get this other error :( .
What you say about enabled behaviours being updated completely makes sense as well...i wish the destroy immediate worked cuz ud get an A+++ in my book(i still give you an A+ for ref and almost practical use of DestroyImmediate(since unity recommends never using it) ).
As for the exact explanation as to why this happens(pipeline) i am still unsure..gonna leave it open for a little while to see if there is more info out there. +1 for your A+ effort/answer. Thank you :), makes sense to me completely(and i thot it would work!)
just saying return; worked...never would have thought of that(or it may have taken me a while : X Thanks for your insight, im sure it will help other down the road