Question on how to use AddComponentObject

Hi,

I have a question about AddComponentObject with the following baker setup:

public class StructureAuthoring : MonoBehaviour
{
    public float hp;
    public NavMeshObstacle testObstacle;

    private class Baker : Baker<StructureAuthoring>
    {
        public override void Bake(StructureAuthoring authoring)
        {
            Entity entity = GetEntity(authoring.gameObject, TransformUsageFlags.Dynamic);

            AddComponent(entity, new StructureData
            {
                hp = authoring.hp
            });

            //AddComponent<NavMeshObstacle>(entity);
            AddComponentObject<NavMeshObstacle>(entity, authoring.testObstacle);
        }
    }
}

public struct StructureData : IComponentData
{
    public float hp;
}

The result is that a component object is added to the entity, but without linking to the actual component. The field is always null (both in the runtime hierarchy and when trying to acces it via code (f.e. with GetComponentObject(entity)).

Am I understanding something inherently wrong about this method (AddComponentObject)?
Currently using Unity 2022.3.34f1 with Entities 1.2.3.

Can anybody get AddComponentObject<SomeClass>(entity, authoring.GetComponent<SomeClass>()); to work? For me this always results in ArgumentException: The given container was null. Visitation only works for valid non-null containers. when I click on the field in the inspector.

Tried it with different classes and a clean project, so I guess it’s a bug atm.

This will only work during runtime for components that aren’t part of the CompanionComponentSupportedTypes.Types array

Any component not part of the CompanionComponentSupportedTypes will be deleted and not end up in runtime.

1 Like

If you’re curious the code located at Library\PackageCache\com.unity.entities@[...]\Unity.Entities.Hybrid.HybridComponents\CompanionComponentSupportedTypes.cs

1 Like

Ahh, I see. Thanks guys.

Is there a(n obvious) way to add the component from the GameObject with the Authoring class to the Entity during runtime?

Or, in general:
Is there a recommended way to somehow link a GameObject (with the Authoring in a subscene) to an Entity (so it can be accessed/modified in a SystemBase)?
Or can I somehow map an Entity to a MonoBehaviour Component in order to disable the latter from a MonoBehaviour?

Baking is an editor-only mechanism to convert human-readable data (authoring data) into ECS data. ECS data is then stored in the subscene. So at runtime, you don’t have any “GameObject contains Authoring class” in any subscene anymore.

To my knowledge, there is no such thing. How you do that really depends on each project. For example, you can have a spawn strategy which involves some hashmap/dictionary or spawner which will let you retrieve a specific gameobject by some ID of your choice.

What do you mean exactly by this?

Just a note of precaution: in an ECS project, any companion GameObject or MonoBehaviour exists just to serve as a presentation, a view. They shouldn’t control anything except GameObject related functionality. So, if you’re going to disable an entity via MB, then you should not. Only ECS system should make change to the ECS world.