Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How to bake an IComponentData that may contain null type?

Discussion in 'Physics for ECS' started by Bob_work, Aug 4, 2023.

  1. Bob_work

    Bob_work

    Joined:
    May 5, 2020
    Posts:
    113
    I had this problem when baking an own Entity:
    struct MyEntity: IComponentData
    {
    public Entity Prefab;
    public int Count;
    public float3 Pos;
    public quaternion Orientation;

    public ConfigurableJoint Joint;//Adding this will cause an error, and changing it to GameObject is the same.
    }

    class MyBaker : Baker<MyEntityAuthoring>
    {
    public override void Bake(MyEntityAuthoring authoring)
    {
    ...
    AddComponent(entity, data);//CS8377: The type "MyEntity" must be a non-nullable type and contain all fields at any level of nesting to be used as a generic type or method "IBaker.AddComponent<T>( Entity, in T)" in parameter "T"
    }
    }

    My purpose is to add a ConfigurableJoint component to the generated Entity. Actually I can add directly on prefab. But adding directly on the prefab has another problem: this Entity will be directly glued to the place where it is generated (0,0,0), that is, on the floor.

    So I'd like to set it so that it doesn't stick to the floor via the action "handle" that the Entity can get to the component when it is generated. But an error is reported, the baker cannot bake a type of data that can have a null value.

    Then I will change another way: I don't add this ConfigurableJoint handle when baking(indeed its still in the prefab), but I just get this Joint in the system to use, but unfortunately it also fails. Errors are of the same type.
    ConfigurableJoint joint = SystemAPI. GetComponentRW<ConfigurableJoint>(myEntity);
    Error: CS8377: The type "ConfigurableJoint" must be a non-nullable type and contain all fields at any nesting level to be used as a parameter in a generic type or method "SystemAPI.GetComponentRW<T>(Entity)" T".

    My purpose is very simple, that is to prevent the Entity from sticking to the floor after the Prefab with ConfigurableJoint generates the Entity. . .
     
    Last edited: Aug 5, 2023
  2. unity_AAA069A8D5FEBD382789

    unity_AAA069A8D5FEBD382789

    Joined:
    Jul 15, 2023
    Posts:
    1
    I think this may help answer you question: https://forum.unity.com/threads/t-is-not-blittable.535874/

    I'm working on a similar problem, and I think a solution might be to create a separate Component for the item you want as nullable. Instead of the value being null, you just don't add it to the Entity. I don't know how this approach would affect performance though and if there is an alternative that may be better.
     
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    330
    Have a look at Managed Components. That should do it.

    The linked documentation provides an example for a component that contains a ParticleSystem, which is a managed type just like the ConfigurableJoint:

    upload_2023-11-6_23-57-58.png