- Home /
OnDestroy in Editor - memory loss issue
When a ExecuteInEditMode-script get the OnDestroy event when the I start debugging, all variables are empty. I want to be able to save the state of my editor when I start debugging.
[ExecuteInEditMode] //Will make the script run in the editor
public class TestEditorMemScript : MonoBehaviour {
int? testval1;
// Use this for initialization
void Start () {
testval1 = 1;
}
void OnDestroy()
{
print("test mem script destroyed");
print("is playing " + Application.isPlaying.ToString());
if (testval1 == null)
{
print("testvalue is NULL");
}
}
}
This gets the following output when I start and exit the debugging
-test mem script destroyed
-is playing False
-testvalue is NULL
-test mem script destroyed
-is playing True
The problem only appears when going from editor- to play mode.
One obvious choice is to save each update, but it seems a bit unnecessary if there is a proper way to do it.
When doing editor scripts, it recommended to use DestroyImmediate. Unity should give you a warning about it.
I think you misunderstood, it is the destroy that happens when Unity removes all running scripts when you press the Play-arrow
Answer by vikingfabian-com · Jun 24, 2014 at 09:43 AM
Solved it!
If running script in the editor:
OnEnable() - create all data here (not in Start())
OnDisable() - is called just before the data is cleared, save current settings here etc.