- Home /
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).
Answer by joseadvrodrigues · Dec 18, 2020 at 12:31 PM
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);
}
}
Answer by Casiell · Dec 15, 2020 at 08:57 AM
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.
I'm not trying to reference the scene object in the prefab asset, but in the prefab instance (called "GameObject" in the scene).
In the Online video 2 in the first six seconds you can see this: in 0:03 you can see that the prefab asset has a null reference in the inspector and in 0:06 you can see that the prefab instance in the scene has a valid reference to the other object in the scene.
And it does get properly serialized. In the same video, when I hit play for the first time, it works properly (doesn't create a new game object because the "Go" field is not null). The problem comes after reloading the scripts and hitting play for the second time: the prefab instance looses it's reference, then on enable it creates another game object.
Why doesn't the prefab instance serialization work on script reload?