Baking component that is disabled by default

Hey,

I’m trying to bake a component who’s default enableable state is disabled. I thought I had it working with the following code:

    public class DisabledComponentTestAuthoring : MonoBehaviour
    {
        class DisabledComponentTestBaker : Baker<DisabledComponentTestAuthoring>
        {
            public override void Bake(DisabledComponentTestAuthoring authoring)
            {
                var entity = GetEntity();
                _State.Ecb.AddComponent<DisabledComponentTest>(entity);
                _State.Ecb.SetComponentEnabled<DisabledComponentTest>(entity, false);
            }
        }
    }

But for some reason, when I have an open subscene in the hierarchy, I get a load of errors about entities not existing and empty object references. Otherwise it works just fine, both with burst and without.

Note, I’m deliberately trying to avoid using systemstate/cleanup components.

Any ideas?

Thanks

Im pretty sure you cant use an ECB during the baking process, as the entity does not fully exsit in the world yet.

The correct baking process would be as follows:

var entity = base.GetEntity();
base.AddComponent<DisabledComponentTest>();
base.SetComponentEnabled<DisabledComponentTest>(entity, false);

More information can be found here:
https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/Docs/baking.md
https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/Docs/examples/baking.md

If you look at IBaker.AddComponent(), it actually uses an ECB. Unfortunately, there isn’t a IBaker.SetComponentEnabled() function and I can’t find any other way to achieve disabling a component at bake time…

Baking System

Thanks for the reply. On first inspection, this looks like exactly what I’m after. I had missed baking systems in my research. However, I’ve just tried one and it seems to add the component correctly that I want to disable but the disabling itself doesn’t actually save to the prefab i.e. it is still enabled. I wonder if this is because the enabled flag is on the chunk and not the component itself… not sure.

The only way I can get it to work is have a system on startup that disables the components before the prefabs are instantiated. It’s not the worst solution but I really expected the baking system to work.

There is issue with enablable components state saving in subscene - close your subscene and enablable component will save properly, if your subscene is opened - it wouldn’t save it correctly.

Wahey! That seems to work, awesome! Thanks a lot for the help :smile: