I'm trying to create a custom object using ThreeJS called Model that is composed of other custom objects I have defined, such as Part. This is where I get my error:
const Model = function() {
this.mesh = new THREE.Object3D();
this.mesh.name = 'model';
//creates instance of part
this.lowerPart = new Part();
this.lowerPart.position.set(1, 2, 3, -75); //TypeError here
this.lowerPart.rotation.set(0, 0, 0);
this.mesh.add(this.lowerPart);
}
However, when I run my program, it says that it cannot read the property 'set' in undefined, referencing this.lowerPart.position.set(1, 2, 3, -75);.
Here is how I basically defined Part:
const Part = function () {
this.mesh = new THREE.Object3D();
this.mesh.name = 'part';
const partShape = new THREE.Shape();
partShape.lineTo( 40, 80 );
partShape.lineTo( 60, 80 );
partShape.lineTo( 60, 100 );
partShape.lineTo( 40, 100 );
//extrude settings defined....
const partGeometry = new THREE.ExtrudeGeometry(partShape, extrudeSettings);
const partMesh = new THREE.Mesh(partGeometry, new THREE.MeshStandardMaterial({
color: 0x1c4bc9,
flatShading: true
}));
this.mesh.add(partMesh);
};
Part.prototype = Object.create(THREE.Object3D.prototype);
Part.prototype.constructor = Part;
Another thing to note in my function Model is that when this.lowerPart is created, it is labeled as unused. I am unsure of the reason since it is type Object3D which has these properties.
The console also proves that this.lowerPart is an instance of Part.
I've looked and tried most of the suggested StackOverflow questions related to my problem. Out of all of them, this seems the most relevant: Uncaught TypeError: Cannot read property 'set' of undefined However, it didn't work for me.
Any suggestions to how I can fix my issue is greatly appreciated!