- Home /
Destroying required components in editor
While in the Editor, I want to be able to call destroy on components. In this situation, two things can happen:
A) That component is required by some other components. In that case, I want to print some message saying that the component can't be removed.
B) That component isn't required by anything else, so it can be safely removed.
After destroying the component, I also want to be able to undo the operation.
Here's the thing: if I use DestroyImmediate, it will work just fine (both A and B), except that I cannot undo the operation. To be able to undo it, I should call Undo.DestroyObjectImmediate. That, however, destroys it without checking if it is required by any other component; which breaks Unity after trying to undo the operation.
I had considered using reflection to make the check myself, which wouldn't be that hard; I just wanted to know if there's any better way around this.
Also, I'm currently using 5.3.4f1
Thanks a lot!
Your answer
![](https://koobas.hobune.stream/wayback/20220612075450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can you use a more performant undo function than RegisterSceneUndo with DestroyImmediate? 2 Answers
Undo.RegisterFullObjectHierarchyUndo not working 1 Answer
Does Undo.RecordObject support marking ScriptableObjects dirty? 2 Answers
How to provide undo for EditorScript changes to a prefab? 1 Answer
Undo.RecordObject isn't working. 7 Answers