Why is my prefab instance loosing a GameObject's reference on script reload?

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).

The gameobject you are referencing is an object on the scene. Prefabs CANNOT have references to objects on scene. Only to objects that are inside their own hierarchy.

From forum user MaskedMouse:

When you set something through script
you have to mark the object as “Dirty”
so that Unity knows this has to be
saved.

Do you need the ExecuteAlways? If so,
mark the game object as dirty. if not,
remove ExecuteAlways.

ExecuteAlways will execute OnEnable /
OnDisable even though the player is
not in a playing state. Any changes
made need to be marked as Dirty.


To fix my problem I just had to call EditorUtility.SetDirty(this) after creating the new GameObject. The new OnEnable method became:

private void OnEnable()
{
    if(go == null)
    {
        go = new GameObject();
        go.name = go.GetInstanceID().ToString();
        EditorUtility.SetDirty(this);
    }
}