- Home /
How to handle a "remove component" from a prefab - Editor callbacks
Hello! I have 3 classes, A, B ,C
And I have a prefab. When I drop the prefab to the scene, and add an A component, A adds B to the game object, and B adds C. When I delete A (click, remove component), B and C are removed too. Because I'm adding this:
[ExecuteInEditMode()]
public class A : MonoBehaviour {
public void OnDestroy()
{
DestroyImmediate(B);
}
}
And, B destroys C the same way.
Now, What I want to do, is to have the same behaviour when I modify the prefab directly.
I have a Resources/ folder, with a prefab, I add an A component, B and C are added. But when I destroy A, B and C don't, because Destroy() isn't called.
How can I do this? Thanks!
Answer by Jojo Batista · Sep 27, 2013 at 05:57 PM
I would like "answer myself" , I actually have to thank yilmaz kiymaz for taking the trouble of answering this, but basically i had to create a class editor for the component
using System;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(A))]
public class AEditor: Editor
{
public void OnDestroy()
{
if(!this.target)
{
A a = (A)this.target;
a.DestroySubComponents(); // This destroys B, and B calls c.DestroySubComponents()
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114850im_/https://answers.unity.com/themes/thub/images/avi.jpg)