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 Quazistax · May 27, 2011 at 10:08 AM · bugmonobehaviourhideleakdirty

Adding MonoBehaviour to HideAndDontSave GameObject makes scene dirty and creates leaks even when component properly destroyed

I want to create hidden game object with script on it, as part of making editor tool. That should not make scene dirty (mark it as it has changes).

Here is test script that creates hidden GameObject, adds script to it, and then destroys script and object.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class MyEditorWindow : EditorWindow
 {
     [MenuItem("Window/MyEditorWindow")]
     static void Init()
     {
         EditorWindow.GetWindow<MyEditorWindow>(false, "Title");
     }
 
     void OnGUI() 
     {
         if(GUILayout.Button("Test Hiden Object+MonoScript Creation"))
             Test();
     }
     
     void Test()
     {
         GameObject hiddenGO = new GameObject();
         hiddenGO.hideFlags = HideFlags.HideAndDontSave;
         
         HiddenMonoScript hiddenMS = hiddenGO.AddComponent<HiddenMonoScript>();        
         hiddenMS.hideFlags = HideFlags.HideAndDontSave;
 
         Object.DestroyImmediate(hiddenMS);
         Object.DestroyImmediate(hiddenGO);
     }    
 }

And here is MonoBehaviour used for test:

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class HiddenMonoScript : MonoBehaviour
 {}



After every recompilation, first execution of Test (click on button in MyEditorWindow) makes scene dirty and when saving it I get a message in console: Cleaning up leaked objects in scene since no game object, component or manager is referencing them MonoScript has been leaked 1 times.

Subsequent Test executions do not mark scene dirty and do not increase number of leaks. Until next compilation occurs and first Test after compilation is executed.

Am I doing something wrong or is it a bug?

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

1 Reply

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

Answer by Bunny83 · May 27, 2011 at 11:20 AM

I guess the problem is that you use ExecuteInEditMode and in addition you destroy it in the same run. ExecuteInEditMode will register the MonoBehaviour to get Start and Update called. You should wait for at least one frame before removing the object. You don't need to destroy the MonoBehaviour because it should be removed when the GO is destroyed.

Since you can't use coroutines in the Editor (except you use your own handler), the best way for ExecuteInEditMode scripts is that you destroy the gameobject in the Start() function of your script. Start is executed at the beginning of the next editor-frame.

If the GameObject exists only for one frame the hideflags are almost useless ;)

edit(copied from comments)
If i run your code above i don't get any warning/error. The scene doesn't get dirty. I put a Debug.Log into the Awake function of the "HiddenMonoScript" and it prints out everytime i click the button.

The error/warning says "MonoScript". That would actually refer to the Textasset and not the MonoBehaviour class that is generated from the MonoScript. Where is your HiddenMonoScript located?


As far as i know namespaces are not that bad, but every class that should be able to get attached to a GameObject needs to be in a seperate file. MonoBehaviours are always associated with their corresponding scripts. A base-class that is derived from MonoBehaviour and that isn't attached directly to a GameObject can be placed wherever you like. Only concrete classes need their own file(classname==filename).
There should be no difference between in-editor attaching (via drag&drop) or using AddComponent from a script.

Comment
Add comment · Show 5 · 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 Quazistax · May 27, 2011 at 11:38 AM 0
Share

The problem was noticed in more complex scenario, where I don't destroy game object or $$anonymous$$onoBehaviour in same frame, so I don't think that's an issue. This test is just for demonstration purpose. I'm aware of not needing to destroy $$anonymous$$onoBehavior before GameObject, but it was more explicit way and I wanted to remove any suspicion that it is destroyed. Also, it does not explain why first Test run makes scene dirty and subsequent ones do not (same for the leak).

avatar image Bunny83 · May 28, 2011 at 04:13 AM 1
Share

If i run your code above i don't get any warning/error. The scene doesn't get dirty. I put a Debug.Log into the Awake function of the "Hidden$$anonymous$$onoScript" and it prints out everytime i click the button.

The error/warning says "$$anonymous$$onoScript". That would actually refer to the Textasset and not the $$anonymous$$onoBehaviour class that is generated from the $$anonymous$$onoScript. Where is your Hidden$$anonymous$$onoScript located?

avatar image Quazistax · May 28, 2011 at 04:53 PM 0
Share

Very good! In example I gave in question, location of script really was a problem. I put Hidden$$anonymous$$onoScript in Editor folder with $$anonymous$$yEditorWindow and somehow Unity didn't report a problem when calling AddComponent... it usually does that when something like this happens.

In my complex scenario problem was something else, but your hint led me to right path to answer :) Problem was I put $$anonymous$$onoBehaviour in namespace. I knew that Unity does not support that, but I thought that "not supporting" part was only about manually adding script to GameObject in editor. Didn't have a clue that in adding by script it could produce something like this... Thanks a lot Bunny83.

Can you make your comment an answer or should I accept your first post?

avatar image Bunny83 · May 28, 2011 at 05:16 PM 0
Share

I've added my comment to the answer ;)
It's strange that Unity doesn't complains that your class is an editor script and can't be attached. $$anonymous$$y Unity(3.0) throws an error like that if you try to attach a $$anonymous$$onoBehaviour that is located in an editor folder.

avatar image Quazistax · May 28, 2011 at 05:50 PM 0
Share

I use Unity v3.3. I made a test now, if I open a project with Hidden$$anonymous$$onoBehaviour already in Editor folder, Test works (with dirty/leak problem). If then I remove script from Editor folder and put it back to Editor folder, running Test reports error in AddComponent.

About namespaces, I put Hidden$$anonymous$$onoBehaviour in namespace $$anonymous$$yNamespace (also add "using $$anonymous$$yNamespace;" to $$anonymous$$yEditorWindow) and Test runs with dirty/leak problem. I tried na$$anonymous$$g script "Hidden$$anonymous$$onoBehaviour.cs" and "$$anonymous$$yNamespace.Hidden$$anonymous$$onoBehaviour.cs" - with same result.

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

2 People are following this question.

avatar image avatar image

Related Questions

Can't use Script Execution Order 1 Answer

How to prevent Update from *not* being called 2 Answers

My dialogue system has a strange bug 0 Answers

Unity Android Screen Glitch 1 Answer

Getting into Monobehavior 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