- Home /
Object created in CustomEditor is leaked into scene when play is pressed.
I want to instance and destroy an object when custom editor is enabled in edit mode (ie, currently selected in scene).
However, if you press play, the created object gets leaked, despite being destroyed in the OnDisable method.
Gif demonstration of issue: https://i.imgur.com/EZOXOmx.gif
Project reproducing the issue: https://github.com/paulmriordan/unity-custom-editor-instance-disable-bug
Using Unity 2018.3.0f2
public class CreateOnClickInScene : MonoBehaviour
{
public GameObject CreatedObject;
}
#if UNITY_EDITOR
[CustomEditor(typeof(CreateOnClickInScene))]
public class CreateOnClickInSceneEditor : Editor
{
private CreateOnClickInScene Target { get { return (CreateOnClickInScene)target; } }
private void OnEnable()
{
if (!Application.isPlaying)
{
Target.CreatedObject = new GameObject("Editor only object");
}
}
private void OnDisable()
{
DestroyImmediate(Target.CreatedObject);
Target.CreatedObject = null;
}
}
Is it not the bool reverse (!) in your OnDisable call of Application.isPlaying?
No, I only want this behaviour when the application is not playing
Exactly, it looks like it says "If the game isn't playing destroy" ins$$anonymous$$d of "if the game is playing destroy"
Your answer
Follow this Question
Related Questions
Finding the exact time in a WaitForSeconds that it is currently at 2 Answers
screenviewcoord[1] < 0 || screenviewcoord[3] < 0 5 Answers
would Unity disable gameobject temporary and automatically when loading a big assetbundle? 0 Answers
OnSceneChange for Don'tDestroyOnLoad GameObjects? 0 Answers
How to add only 1 value in a list in the Update function when a GameObject is active for many frames 0 Answers