I’m upgrading my personal project to Entities 1.2.3. Everything went smoothly until I got to playing the game. This is the error:
ArgumentException: We are reading a UnityEngine.Object but the deserialized index is out of range for the given object table.
Unity.Entities.Serialization.ManagedObjectBinaryReader.Unity.Serialization.Binary.IContravariantBinaryAdapter<UnityEngine.Object>.Deserialize (Unity.Serialization.Binary.IBinaryDeserializationContext context) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/Serialization/ManagedObjectBinarySerialization.cs:246)
Unity.Serialization.Binary.BinaryPropertyReader.ReadValueWithoutAdapters[TValue] (TValue& value, System.Boolean isRoot) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/Properties/Internal/BinaryPropertyReader.cs:234)
Unity.Serialization.Binary.BinaryPropertyReader.ReadValueWithAdapters[TValue] (TValue& value, Unity.Serialization.Binary.BinaryAdapterCollection+Enumerator adapters, System.Boolean isRoot) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/Properties/Internal/BinaryPropertyReader.cs:158)
Unity.Serialization.Binary.BinaryPropertyReader.ReadValue[TValue] (TValue& value, System.Boolean isRoot) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/Properties/Internal/BinaryPropertyReader.cs:137)
Unity.Serialization.Binary.BinaryPropertyReader.Unity.Properties.IPropertyVisitor.Visit[TContainer,TValue] (Unity.Properties.Property`2[TContainer,TValue] property, TContainer& container) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/Properties/Internal/BinaryPropertyReader.cs:115)
Unity.Properties.Property`2[TContainer,TValue].Accept (Unity.Properties.IPropertyVisitor visitor, TContainer& container) (at <78ee2a1d826b40a085095f1f3dc6cfb2>:0)
Unity.Serialization.Binary.BinaryPropertyReader.Unity.Properties.IPropertyBagVisitor.Visit[TContainer] (Unity.Properties.IPropertyBag`1[TContainer] properties, TContainer& container) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/Properties/Internal/BinaryPropertyReader.cs:57)
Unity.Properties.PropertyBag`1[TContainer].Unity.Properties.IPropertyBag<TContainer>.Accept (Unity.Properties.IPropertyBagVisitor visitor, TContainer& container) (at <78ee2a1d826b40a085095f1f3dc6cfb2>:0)
Unity.Properties.PropertyBag.AcceptWithSpecializedVisitor[TContainer] (Unity.Properties.IPropertyBag`1[TContainer] properties, Unity.Properties.IPropertyBagVisitor visitor, TContainer& container) (at <78ee2a1d826b40a085095f1f3dc6cfb2>:0)
Unity.Properties.PropertyContainer.TryAccept[TContainer] (Unity.Properties.IPropertyBagVisitor visitor, TContainer& container, Unity.Properties.VisitReturnCode& returnCode, Unity.Properties.VisitParameters parameters) (at <78ee2a1d826b40a085095f1f3dc6cfb2>:0)
Unity.Properties.PropertyContainer.Accept[TContainer] (Unity.Properties.IPropertyBagVisitor visitor, TContainer& container, Unity.Properties.VisitParameters parameters) (at <78ee2a1d826b40a085095f1f3dc6cfb2>:0)
Unity.Serialization.Binary.BinarySerialization.FromBinary[T] (Unity.Collections.LowLevel.Unsafe.UnsafeAppendBuffer+Reader* stream, Unity.Serialization.Binary.BinarySerializationParameters parameters) (at ./Library/PackageCache/com.unity.serialization@3.1.1/Runtime/Unity.Serialization/Binary/BinarySerialization+FromBinary.cs:28)
Unity.Entities.Serialization.ManagedObjectBinaryReader.ReadObject (System.Type type) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/Serialization/ManagedObjectBinarySerialization.cs:227)
Unity.Entities.Serialization.SerializeUtility.ReadManagedSharedComponents (Unity.Entities.ExclusiveEntityTransaction manager, Unity.Entities.Serialization.BinaryReader reader, Unity.Collections.NativeList`1[T] sharedComponentRemap, Unity.Collections.NativeArray`1[T] unityObjects, System.Void* blobAssetBuffer) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/Serialization/SerializeUtility.cs:2031)
Unity.Entities.Serialization.SerializeUtility.EndDeserializeWorld (Unity.Entities.ExclusiveEntityTransaction manager, Unity.Entities.Serialization.DotsSerializationReader dotsReader, Unity.Entities.Serialization.SerializeUtility+WorldDeserializationStatus& status, Unity.Entities.Serialization.SerializeUtility+WorldDeserializationResult& deserializationResult, System.Int32 externalEntitiesRefRange, System.Int32 sceneSectionIndex, Unity.Collections.NativeArray`1[T] unityObjects) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/Serialization/SerializeUtility.cs:1100)
Unity.Scenes.AsyncLoadSceneOperation+AsyncLoadSceneJob.Execute () (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Scenes/AsyncLoadSceneOperation.cs:149)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/Stubs/Unity/Debug.cs:17)
Unity.Scenes.SceneSectionStreamingSystem:UpdateLoadOperation(SystemState&, AsyncLoadSceneOperation, World, Entity, Boolean) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Scenes/SceneSectionStreamingSystem.cs:655)
Unity.Scenes.SceneSectionStreamingSystem:ProcessActiveStreams(SystemState&) (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Scenes/SceneSectionStreamingSystem.cs:494)
Unity.Scenes.SceneSectionStreamingSystem:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Scenes/SceneSectionStreamingSystem.cs:828)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/SystemBase.cs:418)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/ComponentSystemGroup.cs:723)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/ComponentSystemGroup.cs:681)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/SystemBase.cs:418)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/ComponentSystemGroup.cs:723)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/ComponentSystemGroup.cs:681)
Unity.Entities.InitializationSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/DefaultWorld.cs:169)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/SystemBase.cs:418)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.2.3/Unity.Entities/ScriptBehaviourUpdateOrder.cs:523)
I was able to track it down to this subscene whose only job is to collect all prefabs and convert them into entity prefabs and then manage them in a single container. It’s used such that I could query the Entity of the converted prefab by some ID. I could then instantiate that prefab using EntityCommandBuffer.Instantiate(). This scene has a MonoBehaviour holds a reference to a ScriptableObject that contains all the convertible prefabs. This MB then has a Baker that goes through all the prefabs referenced in the SO and calls GetEntity() on them.
As I was trying some things to fix it, I was able to play the game normally. But it wasn’t long that the error showed again. The only difference is when the subscene is opened or not.
I had the same problem with this subscene when I last upgraded Entities. The fix was to use DependsOn() in the baker code. Could this be related?
Have you encountered something like this? What was your fix or workaround?