- Home /
Build pipeline and wrong values in OnValidate / ISerializationCallbackReceiver
I'm trying to understand how the build pipeline works in Unity after we call 'BuildPipeline.BuildPlayer(...)'.
There has been some weird behavior on our build machine where objects that implemented the ISerializationCallbackReceiver got calls to the OnBeforeSerialize and was causing null references. I've since removed any ISerializationCallbackReceiver to avoid these.
Now I've noticed that OnValidate has the same issue, where it gets called during the build process. In the OnValidate function the code calls a GetComponent() and the values inside the component that has been obtained are empty. (In this particular case a string variable).
#if UNITY_EDITOR
protected override void OnValidate()
{
Something something = GetComponent<Something>();
if (something != null)
{
Debug.Log("OnValidate: " + something.id + ", gameObject: " + gameObject.name + ", scene: " + gameObject.scene.name);
}
}
#endif
This results in the log-file showing lines of objects with empty 'id' values, while the gameobject name and scene name are printed as expected, after the BuildPipeline.BuildPlayer(...) call.
This is also having me believe that there is something going wrong where we are losing references to component references being stored (RuntimeAnimatorController) in classes that are only build through our build machine.
Extra information: I'm on Unity 5.6.3p2, build process is running through TeamCity. Build machine runs on Windows, as a normal process, not a service.
After removing any OnValidate function in the project builds are fine again. It seems that whatever is causing the OnValidate to be called on objects with lost references causes it to be marked dirty and thus being saved.
By removing the OnValidate, I prevent it from being marked as dirty and objects with the lost references don't get saved. It still doesn't answer why OnValidate is being called on an object that's loaded in with lost data though.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143716im_/https://answers.unity.com/themes/thub/images/avi.jpg)