{"id":5015,"date":"2019-06-19T03:32:08","date_gmt":"2019-06-18T19:32:08","guid":{"rendered":"https:\/\/unitycoder.com\/blog\/?p=5015"},"modified":"2019-06-19T03:32:08","modified_gmt":"2019-06-18T19:32:08","slug":"continue-coroutine-after-gameobject-was-disabled","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2019\/06\/19\/continue-coroutine-after-gameobject-was-disabled\/","title":{"rendered":"Continue Coroutine after gameObject was disabled"},"content":{"rendered":"\n<p>So, customer wanted final changes few hours before deadline, to add hide\/show feature (just regular disable-enable gameobject) for some animated UI elements. (Effects were using coroutine to set values and colors).<br><br>To my horror, after you disable and then enable gameobject, the coroutine is dead! And just calling StartCoroutine again resets the values for the loop, so it wouldnt look nice where the fading and UI values would restart everytime.<\/p>\n\n\n\n<p>Enter quick hack, store loop values into variables at OnDisable() and then use them in OnEnable(), before starting coroutine again:<\/p>\n\n\n\n<p><strong>source:<\/strong><br><a href=\"https:\/\/gist.github.com\/unitycoder\/c62fb534982830bcab47a3fc7b182f28\">https:\/\/gist.github.com\/unitycoder\/c62fb534982830bcab47a3fc7b182f28<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"281\" height=\"242\" data-attachment-id=\"5016\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2019\/06\/19\/continue-coroutine-after-gameobject-was-disabled\/image-2-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2019\/06\/image-2.png?fit=281%2C242&amp;ssl=1\" data-orig-size=\"281,242\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2019\/06\/image-2.png?fit=281%2C242&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2019\/06\/image-2.png?resize=281%2C242&#038;ssl=1\" alt=\"\" class=\"wp-image-5016\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>So, customer wanted final changes few hours before deadline, to add hide\/show feature (just regular disable-enable gameobject) for some animated UI elements. (Effects were using coroutine to set values and colors). To my horror, after you disable and then enable gameobject, the coroutine is dead! [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5016,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[804,3],"tags":[1107,1108,1109],"class_list":["post-5015","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripts","category-unity3d","tag-coroutine","tag-restart","tag-setactive"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2019\/06\/image-2.png?fit=281%2C242&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-1iT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=5015"}],"version-history":[{"count":1,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5015\/revisions"}],"predecessor-version":[{"id":5017,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5015\/revisions\/5017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/5016"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=5015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=5015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=5015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}