- Home /
Question by
LnF · Apr 13, 2018 at 11:30 PM ·
editoreditor-scriptingtoggleresetdestroyimmediate
Toggle bool reset after using DestroyImmediate (On Editor Script)
Hi,
I'm trying to create a custom editor script which uses a toggle to Add\Remove a component from an object. After calling DestroyImmediate the addComponent bool is always set to true , if i remove the call then the bool works fine.
Any idea why the bool gets reset after calling DestroyImmediate ?
[CustomEditor(typeof(MyClass))]
public class EditorScript : Editor
{
bool addComponent;
public override void OnInspectorGUI()
{
var myScript = target as MyClass;
addComponent = EditorGUILayout.ToggleLeft ("box2D", addComponent);
if (addComponent) {
if (myScript.GetComponent<BoxCollider2D>() == null)
myScript.gameObject.AddComponent<BoxCollider2D> ();
}
else if(addComponent == false) {
if (myScript.GetComponent<BoxCollider2D>() != null)
DestroyImmediate (myScript.GetComponent<BoxCollider2D> ());
}
}
void OnInspectorUpdate() {
Repaint();
}
}
Comment