I have an [ExecuteAlways] component with a GameObject field and on enable, if the field is null, it creates
a new GameObject and assigns the field to it:
using UnityEngine;
[ExecuteAlways]
public class TestScript : MonoBehaviour
{
bool changeTheNameOfThisFieldToReloadScript;
public GameObject go;
private void OnEnable()
{
if(go == null)
{
go = new GameObject();
go.name = go.GetInstanceID().ToString();
}
}
private void OnDisable()
{
if(go != null)
{
DestroyImmediate(go);
}
}
}
Online video 1 (click here to watch): I create a new GameObject and attach this script to it. It works fine: entering play mode doesn’t create a new game object if one was already created. The same happens after reloading the scripts (to reload the scripts, I just changed a field’s name).
Online video 2 (click here to watch): I now turn the original GameObject into a prefab. It works fine at first; however, when I reload the scripts and enter play mode, the “go” field becomes null and a new GameObject is created! Instead of having one GameObject created by the TestScript, I now have two.
Why is the “go” field null after reloading and hitting play? And why does this only happen when the object is a prefab instance? Is Unity unable to serialize it? Please tell me if you need more information.
You can download the project here (67.07 KB).