Entities Companion Link Error

I found a bug with the Companion Link system in Unity.Entities.Hybrid package. It seems like that companion links generated from prefabs do not work if the scene changes. This only happens in builds, it works fine in the editor.

After digging around I think this must be caused by the way I have things setup. I have an initial scene which has an entity prefab manager. This has all my prefabs baked into entities so I can instantiate them from systems. Once I change to another scene, if I try to instantiate one of those prefabs that has a companion linked Light component child, it throws an error:

NullReferenceException: Object reference not set to an instance of an object
  at Unity.Entities.AttachToEntityClonerInjection.InstantiateCompanionComponentDelegate (System.Int32* srcArray, System.Int32 componentCount, Unity.Entities.Entity* dstEntities, System.Int32* dstCompanionLinkIndices, System.Int32* dstComponentLinkIds, System.Int32* dstArray, System.Int32 instanceCount, Unity.Entities.ManagedComponentStore managedComponentStore) [0x0008a] in .\Library\PackageCache\com.unity.entities\Unity.Entities.Hybrid\Injection\CompanionGameObject.cs:63 
  at (wrapper delegate-invoke) <Module>.invoke_void_int*_int_Entity*_int*_int*_int*_int_ManagedComponentStore(int*,int,Unity.Entities.Entity*,int*,int*,int*,int,Unity.Entities.ManagedComponentStore)
  at Unity.Entities.ManagedComponentStore.Playback (Unity.Entities.ManagedDeferredCommands& managedDeferredCommands) [0x001ee] in .\Library\PackageCache\com.unity.entities\Unity.Entities\ManagedComponentStore.cs:781 
  at Unity.Entities.EntityDataAccess.PlaybackManagedChangesMono () [0x00001] in .\Library\PackageCache\com.unity.entities\Unity.Entities\EntityDataAccess.cs:2645 
  at Unity.Entities.EntityDataAccess.PlaybackManagedDirectly (System.Boolean& didTheThing) [0x00004] in .\Library\PackageCache\com.unity.entities\Unity.Entities\EntityDataAccess.cs:2652 
  at Unity.Entities.EntityDataAccess.PlaybackManagedChanges () [0x00019] in .\Library\PackageCache\com.unity.entities\Unity.Entities\EntityDataAccess.cs:2661 
  at Unity.Entities.EntityDataAccess.EndStructuralChanges (Unity.Entities.EntityComponentStore+ArchetypeChanges& changes) [0x0001d] in .\Library\PackageCache\com.unity.entities\Unity.Entities\EntityDataAccess.cs:482 
  at Unity.Entities.EntityManager.Instantiate (Unity.Entities.Entity srcEntity) [0x0003e] in .\Library\PackageCache\com.unity.entities\Unity.Entities\EntityManager.cs:3504

If I load the new scene additively, it works without error. This leads me to believe that the companion link gameobject that is linked during baking is directly tied to the scene it’s baked in.

Im not sure if this is a bug, or just something that is undocumented. The error should be improved here, it took me a couple days to come to these assumptions.

For now I guess I will change some things around so my prefab manager is in it’s own scene that additively loads other scenes.

1 Like

Hi @WoodsFiend
This post saved me a lot of time figuring out the specifics, thanks! :+1:

My issue was similar except that my prefab manager was in the same scene I was loading. The problem was that I have a ‘loading’ scene (that’s just an interstitial I use) which was flagged as the Active Scene when the new scene with the prefab manager was loading/baking. So subsequently unloading that scene caused the null refs with the Companion Component links.

My solution is to just immediately set the scene with the baked prefabs as the Active Scene when it finishes loading.

SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive).completed += delegate
{
     //make sure we set this new loaded scene active for baking reasons I guess
     SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(index));
};
1 Like