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 wyt000 · Mar 21, 2019 at 02:57 PM · destroy objectfound

Gameobject after destoryed still can be found.

Hi. I created a new unity project and made new simple script below. Once I hit play button in editor, in console window it shows "aaa" . Means unity still found the gameobject that I told it to destroy.

I am using 2018.1.4f1. any idea know whats happening?

 void Start()
 {       
     Destroy(GameObject.Find("aaa").gameObject);
     Debug.Log(GameObject.Find("aaa").gameObject.name);
 }



and even change code to below, debug still showing "aaa".

 void Awake()
 {
     Destroy(GameObject.Find("aaa").gameObject);
 }

 void Start()
 {
     Debug.Log(GameObject.Find("aaa").gameObject.name);
 }
Comment
Add comment · Show 6
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 WarmedxMints · Mar 21, 2019 at 02:58 PM 1
Share

It isn't destroyed until the end of the frame. Try waiting for a frame before looking again.

avatar image wyt000 WarmedxMints · Mar 21, 2019 at 03:09 PM 0
Share

Thank you it works! but is there anyway to not use coroutine to solve this problem?

 void Start()
 {
     StartCoroutine(waitThenGo());
 }

 IEnumerator waitThenGo()
 {
     Destroy(GameObject.Find("aaa").gameObject);

     yield return new WaitForEndOfFrame();

     Debug.Log(GameObject.Find("aaa").gameObject.name);
 }
avatar image WarmedxMints wyt000 · Mar 21, 2019 at 03:14 PM 0
Share

What is the problem? Why are you looking for something after you Destroy it anyway? If you are using GameObject.Find in an update method somewhere, then you need to change that it is should be used as little as possible, if at all.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by fafase · Mar 21, 2019 at 04:40 PM

Destroy does not actually destroy the object but places it in a collection of object to be removed at the end of the frame (after Update). Since your Awake and Start are in the same frame, the object is still alive.

From the doc:

Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

if you need to destroy something then use DestroyImmediately.

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 wyt000 · Mar 21, 2019 at 04:54 PM 0
Share

Thank you this solved my confusion.

avatar image LagSik · Nov 03, 2021 at 01:37 PM 0
Share

Many thanks to you ! This post relieves me of a full head-scratching day of coding and unfruitful debugging. You're my savior !

avatar image
1

Answer by Nivbot · Mar 21, 2019 at 04:10 PM

I don’t need .gameObject at the end of either of those. You can also look in the hierarchy while it’s running an see if “aaa” is really there. I would think you’d get an error trying to log the name of a non-existent object

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 Nivbot · Mar 21, 2019 at 04:10 PM 0
Share

You don’t need not I

avatar image wyt000 · Mar 21, 2019 at 04:46 PM 0
Share

「you don’t need .gameObject at the end of either of those.」 good to know that thanks. :)

and Yes, like you said "aaa" is gone in hierarchy, and there is a error message in console window.

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

104 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

Related Questions

Unity iPhone - Directory.GetDirectories() - None Found? 1 Answer

Destroy a Game object on collision and add it to my Gui??? 1 Answer

Colliding Objects 1 Answer

If xGameobject collides with (Gameobject with label) Destroy it. 2 Answers

Destroying An Instance of an object and childs 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