Change Object using buttons

How to change object using buttons? because I already change the material of it using buttons and then the next target is to change the object and also it can change the materials

Hey, By Object if you mean a 3d model then we can change it in a similar way as we change the material i.e access the render/model component of the entity and simply assign the 3d model Asset’s resource.
For instance, Assuming we have a model component added to the entity then we can change it like

this.entity.model.type = "asset";
this.entity.model.asset = modelAsset;
2 Likes

yes, What code should I use to change the object in one scene

because I only change the scene to change the object and the selection for that object

sorry I’m not a programmer but how can I proceed with this one?

You will have to get a reference from a model asset found in your project. Let’s say you have a model name ‘My Model.glb’ in your assets, then you can do this:

const modelAsset = this.app.assets.find('My Model.glb', 'model');
this.entity.model.asset = modelAsset;

If instead of legacy models you are using the newer render components and assets do this instead:

const renderAsset = pc.app.assets.find('Tree', 'render');
this.entity.render.asset = renderAsset;

the thing is how can I start this code? because I’m only changing the texture not the material

This is the code I am using

//var Framefirst = pc.createScript('Frame first');


Framefirst.attributes.add("textures", {type: "asset", assetType: "texture", array: true, title: "Textures"});

// initialize code called once per entity

Framefirst.prototype.initialize = function() {

    var self = this;

   



    this.textureIndex = 0;



  var myFrameButton1 = this.app.root.findByName('FrameButton1');

    myFrameButton1.element.on('mouseup', this.onFrameButton1BtnPressed, this);

    };

 

Framefirst.prototype.onFrameButton1BtnPressed = function(dt) {



    this.textureIndex = (this.textureIndex + 1) % this.textures.length;



    var texture = this.textures[this.textureIndex].resource;        

   

 

    var meshInstances = this.entity.model.meshInstances;

    for (var i = 0; i < meshInstances.length; ++i) {

        var mesh = meshInstances[i];

        mesh.material.diffuseMap = texture;

        mesh.material.update();

    }

};

Hi @JELS and welcome! Sorry to interrupt, but what are you trying to achieve? Your first post suggests you want to change an entity or model and you are now talking about a texture.

that’s only a example that I show how I change my texture, then I would like to change the object than changing it into a scene

you can check my project here

https://launch.playcanvas.com/1407079?debug=true

as you can see on that scene, I only changing the scene to change the object but my target is, I only need to change one part of the object

Can you share an editor link please? My browser is unable to load the launch page of your project.

https://playcanvas.com/editor/scene/1407079

this is the editor


just like this one

Not sure why, but also the editor is crashing on my device.

Is the part an entity with a model or render component? In that case I would personally add all the different parts to the scene. You can then disable the entities and enable only the entity that you want to show.

As an alternative you can take a look at the tutorial page below. Note that this tutorial uses the new render component that replaces the old model component.

https://developer.playcanvas.com/en/tutorials/using-assets/

yes that’s what I’m thinking, only enable and disable the part

thanks I think I need to find a answer on how to do that

it works that I can change the object but now my problem is how can I change the object without holding a keyboard key and only using the button to change the object

Just like you made the buttons to change your textures.