[SOLVED] Editing Template in runtime

Hello,

I’m working on a project with templates. When I load a template into scene all is fine. Next i try get and edit children entity. And i can`t do, because like template is protected(i guess).
Also I have a version that I’m doing something wrong, because I’m new here (playcanvas).

Main question: how i can edit entity inside template in runtime by script?

Screenshot:
Screenshot_1

Thanks very much!

Are you trying to modify the template asset or the template instance that is in the scene?

Template instance that is in the scene.

Hi @Yurii_A,

From the moment you instantiate a template, it becomes a regular pc.Entity.

That means you can edit it in the same way you would edit any entity:

var entity = templateAsset.resource.instantiate();
this.app.root.addChild(entity);

entity.findByName('Entity_1').name = 'Entity1');
2 Likes

Hi @Leonidas,
Yes you have a right, I can edit name of entity, but I can`t edit model like:
image ;

1)when i do findByTag something wrong. Name is changes but model… not.
2)when i do findByName all work, but this pipeline is not comfortable.

Are you certain the entity has tags? In the code above you aren’t adding/editing any tag on the entity, only the name.

Also what do you mean by … name changes but model… not?

Would you be able to provide a small reproducible project if possible? It can help us give a solution that would work for you :slight_smile:

1 Like

@yaustar yes sure,
function in modelManager.js
https://playcanvas.com/project/770321/overview/test-for-forum

@Leonidas Yes, the entity has tags.
I mean data of entity:
image

I also do not rule out that I am somewhere technically wrong.

The material change looks like it is working to me (Glass to plastic when changing to M sized bottle)

Bear in mind that ModelComponent | PlayCanvas API Reference is not used for model components of type assets.

I’m looking into the tag issue now

2 Likes

findByTag returns an array as there could be more than one entity with the tag:

So the code would be:

var entity_1 = this.entity.findByTag("Bottle")[0];
1 Like

@yaustar thx. My problem is SOLVED :smiley: