Console error when applying material to mesh

Viewed 50

I'm trying to apply MeshStandardMaterial to a mesh of a GLTF imported model. The GLTF model for testing is a simple cube exported from Blender.

After succesfully load the GLTF model I did:

this.material = new THREE.MeshStandardMaterial({
    map: this.texture,
})


this.cubePost2Model = this.resources.items.cubePost2.scene

this.cubePost2Model.traverse((child) =>
{
    if(child instanceof THREE.Mesh)
    {
        child.castShadow = true
        child.material = this.material  // This line throw the error
        console.log(child)              // Returns succesfully one mesh
    }
})

this.scene.add(this.cubePost2Model)

In the previous code, this line: child.material = this.material throw an error in the console:

Uncaught TypeError: Cannot read properties of undefined (reading 'visible')

enter image description here

1 Answers

When an error happens at that point in projectObject(), it means you are assigning undefined to child.material. So it seems this.material does not point to the intended material object.

Related