conversionSystem.GetPrimaryEntity return Entity.Null for scene GameObject

conversionSystem.GetPrimaryEntity return Entity.Null for scene GameObject, it’s not a prefab but i use DeclareReferencedPrefabs to reference it, and alsoi use Convert And Inject GameObject option, i guess there may be some convert ordering issue relate to this, or i’m misuse DeclareReferencedPrefabs?

Got some code?

code was quite simple, what i want is to read transform data from main camera, first i create this MainCameraAuthoring:

struct MainCameraTag : IComponentData { }

[DisallowMultipleComponent]
[RequiresEntityConversion]
public class MainCameraAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
    public Entity Entity;
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        Entity = entity;
        dstManager.AddComponentData(entity, new MainCameraTag());
        dstManager.AddComponentData(entity, new CopyTransformFromGameObject());
    }
}

then is CharacterControllerAuthoring which reads from above entity:

public class CharacterControllerAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
{
    ...
    public GameObject TargetCamera;

    void OnEnable() { }

    void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        if (enabled)
        {
            // [B]where give Entity.Null[/B]
            Debug.Log(conversionSystem.GetPrimaryEntity(TargetCamera));

            dstManager.AddComponentData(entity, componentData);
            dstManager.AddComponentData(entity, internalData);
        }
    }

    public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    {
        referencedPrefabs.Add(TargetCamera);
    }
}

ok after some dig, i found out that in Unity.Entities.Hybrid\GameObjectConversion\GameObjectConversionMappingSystem.cs, it stop me to add GameObject to referencedPrefabs, but with no warning yet:
5172620--513311--upload_2019-11-14_6-12-26.png

5172620--513314--upload_2019-11-14_6-14-11.png

maybe it related to Subscene stuff? so, what should i do to get Entity from scene GameObject?

sorry to bring this up again, i found myself ran into this issue quite often, should’n i do this “link two scene GameobjectEntity together” thing ? to me it’s very normal in Old-non-ecs world…

Subscenes definitely not my forte, but I don’t think you need to use IDeclareReferencedPrefabs for scene references.

subscenes actually works fine, problem came from regular scene object… thx anyway

That’s true you don’t need to declare that unless you need to reference a Prefab.

Unfortunately if you want to reference another object that is outside the subscene that is problematic.
I had similar problem and the way i’ve worked around it was to create a GameObjectConversionSystem for cameras and store their converted entities there (similar to what the mapping system does). Then in your CharacterControllerAuthoring you would have to grab the camera entity from the (Camera)GameObjectConversionSystem map.
It’s a bit hacky but the only way i’ve managed to do this effectively.

agreed, i didn’t do that only because i assume “such a simple problem should have a official solution”.

I’m not sure, I think I have a similar problem which is pretty much just for a trigger and a door. The trigger references the door gameobject and I need to know which entity it will trigger in systems. Both are set to convert and inject.
I have 2 scene gameobjects that have a ConvertToClientServerEntity and a trigger authoring and door authoring comp on it.

The important one:

[DisallowMultipleComponent]
[RequiresEntityConversion]
public class TriggerAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
{
    public GameObject[] triggeredObjects;

    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        var buffer = dstManager.AddBuffer<Trigger_AffectedEntities>(entity);

        for (int i = 0; i < triggeredObjects.Length; i++)
        {
            var convEnt = conversionSystem.GetPrimaryEntity(triggeredObjects[i]);
            Debug.Log("Adding " + convEnt.Index + " to buffer");
            buffer.Add(new Trigger_AffectedEntities() { entity = convEnt });
        }
      
        dstManager.AddComponentData(entity, new TriggerComponent()
        {
            TriggerState = 0
        });
      
        HybridEntity.AddHybridEntity(gameObject, conversionSystem, entity);
    }

    public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    {
        for (int i = 0; i < triggeredObjects.Length; i++)
        {
            referencedPrefabs.Add(triggeredObjects[i]);
        }
    }
}

Output is: Adding 0 to buffer
I have the same problems with the prefab line as @filod
When I comment the line out, 2 door entities are created. One normal, one with prefab component but GetPrimaryEntity is returning back the prefab entity so the systems won’t work on it.

Not sure how I should proceed here. I’m still on 2019.3.0a5 with Entities 0.0.12 preview 33. I can’t update because the whole netcode in the projects breaks sadly. When I’m running into an old problem, please tell me.