Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by unity_qyNAJ9CdbDf9rw · Sep 11, 2019 at 01:18 AM · coroutinedestroy object

Keep doing Coroutine even when destroy game object ??

Hello ! Is there anyway to keep doing coroutine even when destroy object ??? I want my Enemy destroy first, then after 1s I destroy my effect that link to Enemy ??? Thanks for advice !

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

Answer by unityBerserker · Sep 11, 2019 at 10:52 AM

Invoke coroutine from another script. I usually use the couroutine manager, which is attached to the always active gameObject. Below is the CoroutineManager script. Add it to the always active gameObject.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CoroutineManager : MonoBehaviour
 {
     static CoroutineManager instance;
     public static CoroutineManager Instance { get { return instance; } }
 
 
     private void Awake()
     {
         if (instance != null && instance != this)
         {
             Destroy(this.gameObject);
         }
         else
         {
             instance = this;
         }
     }
 }

I created a sample script showing that coroutine will be executed correctly even after destroying the object.

Add the Example script to some gameObject. This gameObject will be destroyed after 1 second. Assign another gameObject to the "efect" field. This gameObject will be destroyed after 2 seconds.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     // reference to "efect" I want to destroy
     public GameObject efect;
 
     void Start()
     {
         //we call ExampleCoroutine and pass references to gameObject "efect".
         CoroutineManager.Instance.StartCoroutine(ExampleCoroutine(efect));
 
         // after 1 second gameObject with this script will be destroyed
         Destroy(this.gameObject,1);
     }
 
 
     IEnumerator ExampleCoroutine(GameObject efect)
     {
         yield return new WaitForSeconds(2);
         // after 2 second we destroy "efect"
         Destroy(this.efect);
 
         while (true)
         {
 
             // we display log to show that coroutine is living
             Debug.Log("I live!");
             yield return  null;
         } 
     }
 }
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 rstrong · May 03, 2020 at 11:25 AM 0
Share

@unityBerserker I'm having a similar issue I need to figure out. What if the game object that doesn't get destroyed is a prefab that will be duplicated a bunch? This solution probably won't work for me. I opened a question about this if you would like to look at my issue.

avatar image
0

Answer by Pangamini · Sep 11, 2019 at 11:28 AM

The @unityBerserker's answer is good. But if all you need is to destroy an object with a delay, you can simply call Destroy(this.effect, 1f); It's a built-in delayed destroy

Comment
Add comment · 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

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

120 People are following this question.

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

Related Questions

Destroying an object with running coroutine 2 Answers

How to destroy children one after another in a coroutine, but with a for loop. 1 Answer

DisplayText info and wait for animation ends to destroyobject ( time extra box) 0 Answers

How to use corutines in a class 1 Answer

How to start a coroutine from Update. 4 Answers


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