[SOLVED] How can I change the model of an entity by code?

I have several assets of type model in an entity. I tried to change the model of that entity with one of the models I assigned.

I tried to do this:

this.entity.model.model = this.avellanas.resource;

But I’m getting an error that says:

Invalid attempt to assign a model to multiple ModelComponents

1 Like

Use the property asset https://developer.playcanvas.com/en/api/pc.ModelComponent.html#asset

How exactly? I already tried changing the asset id but that doesn’t do anything.

EDIT: it changes the model by changing the asset id but now I get a weird bug so the game stops.

There’s also another bug that I don’t get why it happens.
I have an entity with a child. Or at least I see only one child in the editor. But in code there are 2, the entity that I saw in the editor and another child of type GraphNode. That GraphNode child is always first so I use the index 1 to take the entity child. The thing is that when I change the asset id (I only change the asset id) the position of these children change. Is that normal?

The model hierarchy from the FBX are added as GraphNodes that are currently not shown in the Editor.

Can you provide a sample project of this issue for people to look at please?

I may be misunderstanding, but it looks to me like you want to use the Asset Registry.


It’s fine, I already solved it. I didn’t need any asset registry. I just changed the id. and then playcanvas changes the model of the entity to the one of that id.

1 Like