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
0
Question by jkh13 · May 14, 2014 at 10:56 PM · gameobjectdestroy

Cannot destroy objects

I am creating a new game object as follows:

     rangeObject = new GameObject();
     rangeObject.name = Time.frameCount + "";

Further along in my code I am destroying the object as a result of an event:

 public void destroy()
 {
     Debug.Log("destroying range");
     UnityEngine.Object.DestroyImmediate(rangeObject);
 }

After the method above gets called I am still seeing the object in the object hierarchy. The reference to rangeObject hasn't changed in between the create/destroy and I made sure that the destroy line is being called (the log is working as intended). Note that I disable all the components in rangeObject elsewhere before the destroy gets called.

In the object hierarchy the range object named with the frame it was created on is still there and I can reactivate it's components which then work as if nothing was destroyed.

Is there a problem with what I am doing above?

Comment
Add comment · Show 5
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 Scribe · May 14, 2014 at 11:13 PM 1
Share

I'm not certain but perhaps try using Destroy() rather than DestroyImmediate(), DestroyImmediate is usually only used in editor scripts where they can't be delayed to the end of a 'frame' as there isn't a frame. Also it shouldn't be necessary to have UnityEngine.Object but obviously that is just choice in coding convention.

avatar image Kiwasi · May 15, 2014 at 12:11 AM 0
Share

Be very careful with calling DestroyImmediate(). It can also be used to destroy assets permanently. Is designed to only be used in the editor class.

Use Destroy() ins$$anonymous$$d.

avatar image jkh13 · May 15, 2014 at 08:53 PM 0
Share

Oh sorry, it was originally just UnityEngine.Object.Destroy I tried DestroyImmediate to see if it made any difference (it didn't). I am calling this from outside a monoBehaviour which is why I am using the fully qualified name to call it.

avatar image Kiwasi · May 16, 2014 at 04:18 AM 0
Share

Okay, outside of monoBehaviour. That's a significant point, might want to add that to your question.

$$anonymous$$aybe you should be creating the GameObject using instantiate ins$$anonymous$$d of new. Working from memory here but I'm pretty sure Unity throws a fit if you try use the new keyword to create a GameObject inside a monoBehaviour.

avatar image Benproductions1 · May 17, 2014 at 08:34 AM 0
Share

No, it won't throw an error for new GameObject, see the documentation. Are you sure you're destroying the correct object? Can you check whether all the references to the object are nullified after it is destroyed?

0 Replies

· Add your reply
  • Sort: 

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

24 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy all Objects of a Type in Game 1 Answer

Trash Collection For Instantiated Game Object 1 Answer

How to Destroy a gameobject on collision 3 Answers

Only delete the gameobject ONCE 2 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