Can' Find Child Entities Of A Template

Hi, I have instantiated a template at runtime and I am trying to find the children of that instance (by name) )and tween them, but I just keep getting errors that they are undefined:

This part instantiates the entity and tries to assign its child entities (called Explode_Layer_0,Explode_Layer_1 etc)

if( modelAsset && modelAsset.loaded)
 {
        var instance = modelAsset.resource.instantiate();
        this.model_position=this.app.root.findByName("Model Position");
        //this.app.root.addChild(instance);
        this.model_position.addChild(instance);
        
        //get cutaway layers. These models should now exist in the scene
        this.explodeLayer_0= this.app.root.findByName("Explode_Layer_0");
        this.explodeLayer_1= this.app.root.findByName("Explode_Layer_1");
        this.explodeLayer_2= this.app.root.findByName("Explode_Layer_2");
        this.explodeLayer_3= this.app.root.findByName("Explode_Layer_3");
        this.explodeLayer_4= this.app.root.findByName("Explode_Layer_4");
        this.explodeLayer_5= this.app.root.findByName("Explode_Layer_5");
}

But when I try to tween later like this they come back as undefined(Cannot read property ‘tween’ of undefined):

this.explodeLayer_1.tween(this.explodeLayer_1.getLocalPosition()).to(new pc.Vec3(0, this.offsetAmount, 0), this.tweenTime, pc.SineOut).start();

I triple checked and the the names of the entities are all correct and match with the names inside the template.

Any ideas?
Thanks

Hi @Grimmy,

Have you tried searching directly on the instance, instead of root? That would be much faster as well:

this.explodeLayer_0= instance.findByName("Explode_Layer_0");

I still get the same error. :frowning:

Hmm, is there any chance the tween.js library is loaded after that code runs? Tweening works as expected with other entities?

Yeah, I already have tweening on other entities before this that works fine. It seems to be a problem with accessing the entity, for example even this code comes up wit the same error:

this.explodeLayer_1.name="some new name";

That implies that entity of the that name doesn’t exist. Any chance of sharing a build link or you can use my devtools to print out the hierarchy of the scene into the console (https://github.com/yaustar/yaustar.github.io/tree/master/playcanvas-devtools)

2 Likes

Okay I sent an invite. Sorry for the delay but I was putting out a fire (quite literally in my house! - all safe now). To see the error just run it and choose Purple> Purple Original (You’ll see what I mean.)

The script with the issue is called model-screen-manager.js

Looking at the scene, there isn’t any nodes named ‘Explode_Layer_0’ to ‘Explode_Layer_5’

As it’s the mattress that we are looking at:

image

No child entities are here in the scene with the template and no other graph nodes at runtime on the model

There’s only one mesh instance in the GLB

Importing the OBJ in Blender only shows one node too

Did you mean to instantiate ‘Test_Cutaway’ instead?

image

2 Likes

Yes!. Apologies I was looking at the wrong mattress all along! If you look at the Purple Hybrid 4 it works fine. Thanks for your help and apologies for being stupid with this one.

:blush:

1 Like