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 neginfinity · Mar 31, 2015 at 10:31 AM · c#gameobject3d

Unity 5: Clean way to manage dynamically created GameObjects

In Unity 5, what is "clean" way to manage dynamically created game objects?

I've written a component (`MonoBehavior`) that creates/destroys several GameObjects. The objects are loaded as part of custom customization system that selects portions of character - hair/clothes, etc. Meaning they're visible to player, visible in editor, but are not supposed to be editable in editor. The objects being loaded are meshes with skeletons.

The script behaves in this fashion:

  1. Loads GameObjects from Resources (exact object is determined in script, they are not prefabs)

  2. Attaches them to some portion of the scene (not necessarily to its own node)

  3. Deletes when destroyed.

Deletion:

 protected void unloadLastResource(){
     if (lastResourceInstance){
         if (Application.isEditor){
             GameObject.DestroyImmediate(lastResourceInstance);
         }
         else
             GameObject.Destroy(lastResourceInstance);
         Resources.UnloadUnusedAssets();
         lastResourceInstance = null;
     }
 }

Creation:

     GameObject target = getEffectiveTargetNode();
     Object resource = Resources.Load(newResourceName);
     instance = Instantiate(resource) as GameObject;

     instance.hideFlags = HideFlags.HideAndDontSave;
     instance.transform.parent = target.transform;
     instance.transform.localPosition = Vector3.zero;
     instance.transform.localRotation = Quaternion.identity;
     instance.transform.localScale = Vector3.one;

Destruction handler:

 void OnDestroy(){
     unloadLastResource();
 }
 

That seems to work fine in editor, but when I switch from gaming mode back to edito mode, I get a lot of warnings:

 Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.
 UnityEngine.Object:DestroyImmediate(Object)

And I get bunch of new object trees (the ones that are supposed to be deleted - trees originate from object that was loaded along with original "resources" and was attached) at the top level of scene hieararchy.

So, how do I cleanly handle dynamically created gameobjects?

I need to know which flags I need to set, and which steps I should do to ensure that object does not "leak" into scene and is properly destroyed when I delete component that created it.

Advice?

Comment
Add comment · Show 3
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 dorpeleg · Mar 31, 2015 at 01:02 PM 0
Share

As the warnings suggests, you are doing DestroyImmediate inside OnDestroy...

Don't do that.

I don't understand why you are using DestroyImmediate anyway... if this is a runtime script and not editor script there is no reason to use DestroyImmediate.

avatar image neginfinity · Mar 31, 2015 at 01:26 PM 0
Share

@dorpeleg: The script controls customization of a character. Selected parts are visible in editor. Without DestroyImmediate they don't get properly cleaned up in editor. Editor script is derived from this class. Also, warning suggest not to use those methods on the same object

avatar image Glurth · Mar 31, 2015 at 02:45 PM 0
Share

Not very familiar with this question's topic, but regarding dorpeleg's comment and your response: Perhaps you could use the .isPlaying function to decide weather to call destroy or destroyimmidiate

1 Reply

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

Answer by neginfinity · Mar 31, 2015 at 03:17 PM

I've figured it out.

The problem is caused by this line:

 instance.hideFlags = HideFlags.HideAndDontSave;

In hierarchy, this flag affects only ONE object and does not affect object's children. As a result, if you set this flag for the root object in hierarchy and scene gets saved, root will be destroyed, but its children will get serialized (meaning they'll appear in inspector upon scene reload) AND you'll get tons of warnings about destroying same object many times.

To deal with the problem, it is necessary to walk through the entire hierarchy and set the flag to all objects in hierarchy. Which fixes the problem and eliminates all errors.

 void makeHierarchyHidden(GameObject obj){
     obj.gameObject.hideFlags = HideFlags.HideAndDontSave;
     foreach(Transform child in obj.transform){
         makeHierarchyHidden(child.gameObject);
     }
 }

 .....
                 makeHierarchyHidden (newObject);
 .....

It is unclear if this only happens when loading non-prefabs from disk, or just in general case with all hierarchical objects.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Jump on the ground 2 Answers

How do i get the LocalSize of an object? 2 Answers

How to move an object in a looped path but in random speed in 3D?, or How to move an object in circular path in random speed each time? 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

SetActive is not working 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