How to find deactivated component in children

I have the following code to find all models in the children of a template:

let inst = templateAsset.resource.instantiate();
let models = inst.findComponents('model');

I’m finding that if I have a child entity disabled, models attached to that entity will not be found. My workaround at the moment is to just never have disabled entities in my templates.

How can I include disabled entities in this search?

Hi @Quincy,

A way is to use the more generic find() method:

let models = inst.find(o => o.model);

This will return all entities that have a model component, no matter its state.

Still I am surprised that findComponents returns only enabled components, that isn’t stated in the docs.

Yes, internally findComponents() uses find(), and it will return all components not only enabled ones.

    findComponents(type) {
        const entities = this.find(function (node) {
            return node.c && node.c[type];
        });
        return entities.map(function (entity) {
            return entity.c[type];
        });
    }

I can’t repro the issue in this simple project test with deactivated components: https://playcanvas.com/editor/scene/1275110

I’ve just reread that you use disabled entities. In which case, it is very likely that until the entity is first enabled, the components are not added to the entity as an optimisation.

Hmm, that doesn’t look right either. I have a deeper look

Just tried it in the same project where Backing entity in the template is disabled: https://playcanvas.com/editor/scene/1275110

It finds the component fine looking at the debug output