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 Dray · Jan 17, 2018 at 07:48 PM · c#gameobjecteditoreditor-scriptingplaymode

DestroyImmediate(component.gameObject) destroys component but not gameObject

I got a weird problem with the DestroyImmedeate() function. First off, here's a version of the script that I reduced to the problematic part:

 using UnityEngine;
 using UnityEditor;
 
 [ExecuteInEditMode]
 public class MyScript : MonoBehaviour {
     public MyComponent myComponent;
         
     void Awake() {
         myComponent = Instantiate<GameObject>(
             Resources.Load<GameObject>("Prefabs/MyComponentPrefab"),
             this.transform
         )
         .GetComponent<MyComponent>();
     }
     
     void OnDestroy () {
         if (EditorApplication.isPlaying)
         {
             Destroy(myComponent.gameObject);
         }
         else
         {
             DestroyImmediate(myComponent.gameObject); // <- this behaves wrong some times?
         }
     }
 }


The problem occurs when I switch from edit to playmode. So basically, what I expect to happen, is that when the OnDestroy() function gets called, the GameObject attached to the myComponent variable gets removed completely.

Then, Awake() is called and a new instance is created, again, as a child in the hierarchy.


For some reason though, DestroyImmediate(myComponent.gameObject) is called but only the component gets removed. What stays is an GameObject containing all the other components that were attached to it despite MyComponent and it is still listed as a child of MyScript in the hierarchy.


If everything worked the whole GameObject should be gone so what am I missing here?

Comment
Add comment · Show 7
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 Glurth · Jan 17, 2018 at 10:21 PM 1
Share

I notice your code in inside OnDestory. This is invoked when the $$anonymous$$yScript component is about to be destroyed. This means the $$anonymous$$yScript component will be removed from its game object, and possibly, that its gameobject is also being Destroyed (which would also destroy its children). I don't see why you would expect this awake function to be called again, though.

WHY this doesn't also remove the mycomponent game-object, I'm not sure. But I DO see that you don't actually check to confirm the myComponent reference is valid. I would recommend a Debug.Log(myComponent.name); or Debug.Log(myComponent.gameObject.name); command as the first step in troubleshooting

avatar image Dray Glurth · Jan 18, 2018 at 09:35 AM 0
Share

Oh I totally overlooked your comment yesterday, I'm gonna check if the reference isn't right at that point anymore, that could be it!

EDIT: Nope, the reference seems to be right when I give my manager a random name and then output it right before calling DestroyImmediate :(


According to the functions that get called, everything works as I expected after looking at the ExecutionOrder section in the docs:

Execution Order

I figured Awake and OnDestroy are the events that I want to use since OnEnable and OnDisable should only "pause" the managers but not remove them completely.

This is how my IDE looks when I press play:

alt text

The console output confirms that the functions are called in the right order (cleared it before pressing play). In the hierarchy you can see the "duplicate" Pooling$$anonymous$$anager with no components in the inspector. The latter is a problem because one more of these empty objects gets created everytime I press play. Even when making them hidden and putting t hem to DontSave, that's a pretty ugly "memory leak" that I can't live with

avatar image Bonfire-Boy · Jan 18, 2018 at 10:10 AM 1
Share

Am I missing something? Because of how you create it in Awake(), myComponent.gameObject is a child of the GameObject that's being destroyed when that OnDestroy() function is called. So surely it's going to be destroyed anyway...? Explicitly destroying it in the OnDestroy() looks superfluous to me.

avatar image Dray Bonfire-Boy · Jan 18, 2018 at 10:20 AM 0
Share

It's a child in the hierarchy but the component I'm trying to destroy is not attached to the GameObject executing that code itself.

The GameObject "$$anonymous$$yScript" stays in the scene until the developer removes it but from Unitys architecture, as far as I understood it, GameObjects are getting created and deserialized or destroyed and serialized whenever an application starts or ends and therefore those functions are called even when $$anonymous$$yScript is not visibly being destroyed while switching modes.

All I really need that's not working right currently is the GameObject containing "$$anonymous$$yComponent" being destroyed before "$$anonymous$$yScript" is.

avatar image Bonfire-Boy Dray · Jan 18, 2018 at 10:50 AM 1
Share

1) You're not trying to destroy a component, you're trying to destroy a gameobject (that's the whole point, right?)

2) That gameobject is instantiated as a child of the gameobject executing that code. So it will be destroyed anyway.

If you need a child to be destroyed before its parent, I'm not sure that doing it in the parent's OnDestroy makes sense, because the parent is already being destroyed at this point.

avatar image Dray Bonfire-Boy · Jan 18, 2018 at 12:03 PM 0
Share

Actually you're totally right, this just led me to the solution!

So in fact, when Unity "stops", it serializes and then destroys everything so this destroy call is redundant, I could have noticed. When the application starts, the previously serialized data gets converted back into objects and additionally my custom create call is executed, that's why I'm getting two objects.

Now the reason why that component disappeared seemed to be that I set my DontSave-hideflag to the component ins$$anonymous$$d of the GameObject by accidet so the GameObject still got included into the serialization process. I should have included that in my question but I didn't expect it to have anything to do with the issue. Anyhow, with the DontSave-hideflag set to the GameObject itself, everything works and the destroy call can completely be removed (theoretically, I still want it to be there for certain reasons but that's another topic).


Long story short; my problem is solved. Thanks a lot everyone! :) Now I can't accept multiple answers unfortunately even if they all helped me but if you convert this comment, I'm accepting this one since it brought me to my final solution

avatar image Glurth Dray · Jan 18, 2018 at 03:51 PM 0
Share

"I should have included that in my question but I didn't expect it to have anything to do with the issue. "
Welcome to the club ;) glad you got it goin'.

1 Reply

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

Answer by Bunny83 · Jan 17, 2018 at 09:30 PM

You're doing some dangerous things here. ExecuteInEditMode is not ment to implement editor functionality. It's just ment to provide live feedback while in edit mode. When you enter playmode at the time the editor scene is destroyed it got already serialized. That means when the scene gets deserialized at the beginning of playmode the object will probably be recreated from the serialized data.


This line:

 DestroyImmediate(myComponent.gameObject); 

actually has no direct connection to myComponent since the expression myComponent.gameObject is a gameobject reference. So it's impossible that it somehow is able to only destroy your component.


If you need some gameobjects / component at edit time you should set proper hideflags. However from the posted code it's hard to determine the purpose of the script. Where is that script even attached to?

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 Dray · Jan 18, 2018 at 12:15 AM 0
Share

Oh look its bunny :D thanks for helping me out again!

So this script is part of a custom terrain system I'm building and I'm using it to spawn different $$anonymous$$anagers that take care about object poolig, buffering lod changes and stuff like that. All that functionality is not interactive at all but rather constantly running in the background and I need it to run in the Editor aswell as in the playmode. Depending on which of those, I need to use a different camera and different Update loops. Thats pretty much the background and the reason why I want this object freshly initialized on every kind of startup. I dont want these $$anonymous$$anager classes to be always present, they are going to be set to HideAndDontSave when fully implemented so that only the actual Terrain component is visible if that makes any sence?


Now I think serialization is a good hint, maybe that behavior comes from that corner.. The thing I'm not getting is why is this destruction line always works and destroys the whole GameObject despite in that one situation where I need it

avatar image Dray · Jan 18, 2018 at 12:26 PM 0
Share

Actually, since this was the first answer and is just as valid as the other comments, I'm just accepting this one for now. Thanks again @Bunny83 @Glurth @Bonfire-Boy! You guys helped me a lot :)

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

457 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 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 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 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

How to access Custom Inspector own gameObject 1 Answer

How to add a component on a GameObject in Custom Inspector 1 Answer

Close script tab in VS, from script 0 Answers

How to create instance from model without creating a clone? 2 Answers

Change default sprite position when dragged into Hierarchy 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