I've seen this reported a lot of times breaking builds
IndexOutOfRangeException: Index was outside the bounds of the array.
at Unity.Scenes.SerializeUtilityHybrid.DeserializeObjectReferences (Unity.Scenes.ReferencedUnityObjects objRefs, UnityEngine.Object[]& objectReferences) [0x0014e] in .\Library\PackageCache\com.unity.entities@1.3.2\Unity.Scenes\Hybrid\SerializeUtilityHybrid.cs:146
However was never sure what was causing it. However someone triggered this at work yesterday so I had the opportunity to track down the problem.
An hour later it was clear the problem is using a MonoBehaviour in UnityObjectRef. Repro is easy
public struct ReproComponet : IComponentData
{
public UnityObjectRef<Repro> Value;
}
public class ReproAuthoring : MonoBehaviour
{
public Repro Object;
private class ReproBaker : Baker<ReproAuthoring>
{
public override void Bake(ReproAuthoring authoring)
{
var entity = this.GetEntity(TransformUsageFlags.None);
AddComponent(entity, new ReproComponet { Value = authoring.Object });
}
}
}
public class Repro : MonoBehaviour
{
public int Value;
}```
You can actually repro his in editor by making a tiny chagne to entities package
Disable the condition Line 114 in SerializeUtilityHybrid.cs so it uses the build path
#if UNITY_EDITOR && !UNITY_DISABLE_MANAGED_COMPONENTS && false // Edited so build path executes in editor as well for easy repro
I've reported this here: IN-88787
Looking at source it makes sense why this breaks and you can't easily restrict this, so if a proper fix isn't easy to implement at least adding a check somewhere in entities to warn users this isn't allowed would be nice.
I suspect this might also break on other Component types, such as MeshFilter etc but I haven't tested to confirm.