Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by hijinxbassist · Jun 23, 2012 at 04:18 AM · destroyenabled

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.

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
2
Best Answer

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.

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 hijinxbassist · Jun 23, 2012 at 07:42 AM 0
Share

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

avatar image
1

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.

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 hijinxbassist · Jun 23, 2012 at 07:36 AM 0
Share

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!)

avatar image hijinxbassist · Jun 23, 2012 at 07:44 AM 0
Share

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Two objects are placed at the same place. How to destroy one and keep another? 1 Answer

How to respawn a box once destoyed??? 1 Answer

How to distroy the script 2 Answers

Destroy Turret with machine Gun 0 Answers

Destroying game object next to player 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