#[BUG] UnityObjectRef<T> where T : MonoBehaviour breaks subscenes in builds IN-88787

1 messages · Page 1 of 1 (latest)

devout plank
#

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.
visual falcon
#

I wonder if UniyObjectRef<SpriteRenderer> also causes this issue. I vaguely recall that exception the first and also last time I tried it.

#

Has to be a base asset (Mesh, Material, Gameobject [prefab] etc)
I guess anything that is UnityEngine.Component can't be used.

devout plank
brittle wigeon
#

"If multiple classes are defined in your script, Unity selects the class with the same name as the file."

devout plank
#

In the actual submitted bugreport it's in its own file

brittle wigeon
#

what's the bug number?

#

ah you said above