Best Way to Switch a Model

Hi I have an entity with a bunch of scripts attached. At runtime I want to modify the entity so that it is exactly the same apart from the model is swapped with a different model(scripts remain the same etc).

I have tried modifying the asset of the render component with a straight glb but it doesnt work. It seems I manually need to replace both the material and the mesh…and the mesh normally has some unfriendly name like mesh.002 etc. I just want to be able to replace car with motorbike for example; a direct reference to a glb container.

What is the best way to do this?

Cheers

Hi @Grimmy,

To update the render asset referenced on any render component, you do this:

this.entity.render.asset = renderAsset;

But if you’ve loaded a GLB container that include multiple things (entities, models, materials etc), then you may need to follow a different course:

  1. Remove the entity that you attached when calling the instantiateEntity() method on the GLB container (that will remove all child entities and models).

  2. Instantiate a new entity from the new GLB container you loaded and attached it to the hierarchy.

Thanks. Yes, Im loading a glb with multiple parts . My code just does this (below). There are no errors but no mesh appears. Im guessing Im missing the instantiateEntity() method. Could you explain that more ? I dont want to instantiate any new entities, just replace the components that already exist within the existing entity.

this.asset_variations[current_game_state].ready(function () 
        {
            this.entity.render.asset = this.asset_variations[current_game_state].resource;
        }.bind(this));

Apologies, it looks like I didnt need the .resource appending at the end.