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
1
Question by Byterunner · Mar 09, 2011 at 11:35 PM · editorassetpostprocessordestroyimmediate

Editor.DestroyImmediate crashes Unity. Am I using it wrong?

I have a number of empty group nodes in Maya that contain user-data. I'm using an Editor Script to call OnPostprocessGameObjectWithUserProperties to use this data. That all works perfectly fine and my data is used well. Now I want to get rid of the empty game objects so that they're not cluttering up my model's hierarchy, but it always seems to crash Unity. I have:

using UnityEngine; using UnityEditor; using System.Collections;

public class ModelImport : AssetPostprocessor {

 void OnPostprocessGameObjectWithUserProperties( GameObject go, string[] names, object[] values) 
 {
     // Do some stuff
     Editor.DestroyImmediate(go, true); // This line causes Unity to crash to desktop
 }

}

Anyone have an idea what I'm doing wrong, or rather, what I should be doing instead?

Comment
Add comment · Show 2
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 by0log1c · Mar 09, 2011 at 11:38 PM 0
Share

Just wondering: what's the 'true' for? I've just used a DestroyImmediate(theGameObject); and everything works fine everytime. Though Editor script errors do tend to screw/crash Unity.

avatar image Byterunner · Mar 10, 2011 at 04:30 PM 0
Share

Oh, that I forgot to take out, but it doesn't change anything about it not working. Putting true there allows DestroyImmediate to destroy assets, rather than just GameObjects. It defaults to false, and I had put true in an attempt to get things not to crash, but there's no difference.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 10, 2011 at 01:08 AM

OnPostprocessGameObjectWithUserProperties iterates through all containing objects when importing an asset. I guess when you destroy it here Unity's current iteration will continue on the destroyed object. I don't even understand why you want to destroy the whole asset in the postprocessor...

Well, if you really want to destroy it (maybe due to some user data set) you could try to destroy it in AssetPostprocessor.OnPostprocessModel. You just have to keep track of your condition. If that doesn't work either you could create a temp GO with a special script (ExecuteInEditMode) and let it delete the asset (and itself). Well, it should even be possible to add a selfdestruct script to the asset but i think OnPostprocessModel should work.


edit
You can try AssetDatabase.DeleteAsset. It should also be capable of deleting just a subobject. If it doesn't work just use a List<> to store the Objects you want to delete and do it later, maybe in AssetPostprocessor.OnPostprocessAllAssets. Just delete all objects in your list.

using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic;

public class ModelImport : AssetPostprocessor { private List<GameObject> m_ObjectsToDelete = new List<GameObject>();

 void OnPostprocessGameObjectWithUserProperties( GameObject go, string[] names, object[] values) 
 {
     // Do some stuff
     m_ObjectsToDelete.Add(go);
 }
 void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
 {
     foreach(GameObject go in m_ObjectsToDelete)
         DestroyImmediate(go,true);
     m_ObjectsToDelete.Clear();
 }

}

I haven't tested it yet, but it should work.

Comment
Add comment · Show 8 · 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 Byterunner · Mar 10, 2011 at 04:33 PM 0
Share

I don't want to destroy the whole asset, I only want to remove that one GameObject node that has the data on it. For example, my model consists of three meshes and two empty group nodes with user data on it. I want to remove those two empty group nodes so that when I import the model and expand the hierarchy, I only see the three meshes. AssetPostprocessor.OnPostprocess$$anonymous$$odel won't work because that works on the entire asset, not on the individual game objects that contain the data.

avatar image Bunny83 · Mar 10, 2011 at 05:18 PM 0
Share

edit* ;) Did you even check for any special userdata or do you just want to remove everything that contains userdata?

avatar image Byterunner · Mar 10, 2011 at 06:47 PM 0
Share

Yes, I check to make sure I actually want to remove this particular game object. That's what part of the "// Do some stuff is", I just simplified it, because that's not where the problem is. I had thought about doing a list and deleting them later, but didn't think to put it in OnPostprocessAllAssets. Unfortunately, that doesn't seem to work. I get an error on the List declaration line: CS0246 The type or namespace name List1' could not be found. Regardless of that, OnPostprocessAllAssets doesn't even seem to get called. I tried putting some Debug.Log statements in it to test, but nothing.

avatar image Bunny83 · Mar 10, 2011 at 07:42 PM 0
Share

Well, i just forgot that in C# you need to add using System.Collections.Generic;

avatar image Byterunner · Mar 10, 2011 at 11:37 PM 0
Share

So the error is gone, but its still not doing anything for OnPostprocessAllAssets. It's like it never actually gets called. I've found a work-around by na$$anonymous$$g the created nodes specifically, and then in a separate OnPostprocess$$anonymous$$odel routine that is called later, I look for game objects named as such and remove them. I don't like doing it that way because it feels wrong, but it's functional for now. Interestingly enough, Editor.DestroyImmediate works in that scenario, but does not in the original scenario.

Show more comments
avatar image
0

Answer by TomasRiker · Apr 20, 2012 at 12:07 PM

I know this question is old, but I just wanted to add a comment, since it is still useful! I had the same problem, and when I tried to put the game objects to be deleted into a list and delete them in OnPostprocessAllAssets, nothing happened.

By the way, the method has to be static, otherwise it won't be called!

However, deleting the object in there didn't work.

Therefore I also rename my object to "DeleteMe" and check for the name in OnPostprocessModel.

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
avatar image
0

Answer by _watcher_ · Jun 27, 2016 at 11:42 AM

Destroy destroys asynchronously.

DestroyImmediate works in test environment, but crashes my target App.

Editor.DestroyImmediate works in test environment, but crashes my target App.

OnPostprocessAllAssets is just bad implementation to do something that should be done with synchronous Destroy.

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

2 People are following this question.

avatar image avatar image

Related Questions

Help with AssetPostProcessor and keyframes? 0 Answers

Metadata not being saved on custom file types 0 Answers

Importing lpc assets 0 Answers

How to avoid error message when using DestroyImmediate in Editor 0 Answers

adapting replace prefab example 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