I have a MonoBehaviour that has an ExecuteInEditor attribute. When I run the game from editor, it crashes deterministically on OnEnabled() because all the data members are in their uninitialized state. I have a following test script that replicates this issue:
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class ExecuteInEditorTest : MonoBehaviour
{
List<string> callHistory = new List<string>();
System.Object testMember = null;
void Awake()
{
callHistory.Add("Awake");
testMember = new System.Object();
}
void Start()
{
callHistory.Add("Start");
}
void OnDestroy()
{
callHistory.Add("OnDestroy");
}
void OnEnable()
{
callHistory.Add("OnEnable");
if (testMember == null)
{
foreach (string call in callHistory)
{
Debug.Log(call);
}
}
}
void OnDisable()
{
callHistory.Add("OnDisable");
}
}
When I run a test scene from editor, the script logs
Awake
OnEnable
Start
OnEnable
or
Awake
OnEnable
OnEnable
Any idea who sets the testMember to null and when? And why the call history is not always the same?
The test scene has just one empty game object with this MonoBehaviour and the default camera.
Cheers,
Antti
This is an example of a great question. You've distilled the problem down to its core, provided code to recreate it, and showed that you worked yourself to try to figure it out. Nicely done.
– Dave-Carlile