Reputation: 25
I'm getting the console error:
TypeError: scene.getObjectByName(...) is undefined
on these two lines:
scene.getObjectByName('teapot').rotation.x += 0.005;
scene.getObjectByName('teapot').rotation.y += 0.005;
The relevant code where I'm loading the object looks like this:
var loader = new THREE.OBJLoader();
loader.load('obj/teapot.obj', function(object) {
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material = phongMaterial;
}
});
object.scale.x = 2;
object.scale.y = 2;
object.scale.z = 2;
object.name = 'teapot';
scene.add( object );
});
The scene itself is just a simple teapot rotating around, everything is rendering and behaving as it should as far as I can see.
I'm just wondering why the error?
Upvotes: 1
Views: 2216
Reputation: 15604
var loader = new THREE.OBJLoader();
loader.load('obj/teapot.obj', function(object) {
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material = phongMaterial;
}
});
object.scale.x = 2;
object.scale.y = 2;
object.scale.z = 2;
object.name = 'teapot';
scene.add(object);
scene.getObjectByName('teapot').rotation.x += 0.005;
scene.getObjectByName('teapot').rotation.y += 0.005;
});
You are getting scene.getObjectByName('teapot') == undefined
because you are calling that before teapot object added to scene. Call that inside load callback, so you will get teapot object and then add rotation to it.
Upvotes: 1