I have a car model which has 20 materials in it and I want to change the 3rd material in the array to something else. But my code is not working its showing(Uncaught TypeError: Cannot read properties of undefined (reading ‘material’).
Here is my code any kind of help will be appreciated
Hi @Vivek_Chaini and welcome! I’m not totally sure, but I think it’s because you search for a render component to get the material while you use a model component for your car entity.
@Albertos can you show me the code for it I am very new in JavaScript and this playcanvas engine. It will be very helpful you can take the reference from my code. I have been struggling for past 2 days.
Oh wait. Seems that meshInstances is undefined now and that’s probably because you use the render component at the moment. If you use a render component you need to use findComponent('render') and if you use a model component you need to use findComponent('model').
@Kulodo133 Yea its confusing.What i need to do is change the one specific material from material array of the model. I tried different things but nothing seems to work im very new at this. Please tell me how can i achieve this.
@Albertos I used asset type as model and findComponent(‘model’) its showing this error i click the button “Uncaught TypeError: Cannot read properties of null (reading ‘meshInstances’)”
The button event listeners were being added in the update function of the script. Which means there was listeners being added every frame. I’ve moved them to initialize so they are only called once.
mats1.meshInstances[2].material = this.mat2 - this.mat2 is the script attribute that references the material asset, not a material. Changing it to mats1.meshInstances[2].material = this.mat2.resource fixes the issue.