user6600549
user6600549

Reputation:

How to access a scene by name or id in three.js, no scene.childs just scenes

I try this three.js how to get scene by id(name)

and I get this error

Uncaught TypeError: THREE.getObjectByName is not a function

why I can't access a scene by name or id????

the code is that

// setup scenes
var scene-1 = new THREE.Scene();
scene-1.add('cube_red');

var scene-2 = new THREE.Scene();
scene-2.add('cube_green');

// get scenes by name
var sceneObj = THREE.Object3D.getObjectByName("scene-1");
console.log(sceneObj.userData.someProp);

// or 

selectedScene = THREE.Object3D.getObjectByName("scene-1");

Any suggestion?

Upvotes: 0

Views: 1496

Answers (2)

Lewy Blue
Lewy Blue

Reputation: 618

The code you posted is not valid Javascript. But this gives the same error you are getting:

var scene1 = new THREE.Scene();
var sceneObj = THREE.Object3D.getObjectByName("scene1");

You need to instantiate an object before using getObjectByName. This will work:

//create a Scene
var scene1 = new THREE.Scene();

//create an Object3D
var object1 = new THREE.Object3D();

//give the object a name. By default objects in three don't have names
//but you can add one manually
object1.name = 'obj1';

//add the object as a child of the scene
scene1.add(object1);

//now search through the scene's children for an object called 'obj1'
var obj1 = scene1.getObjectByName('obj1');

console.log(object1 === obj1); //true

Upvotes: 0

gaitat
gaitat

Reputation: 12632

You cannot have -1 or -2 in a variable name.

Upvotes: 1

Related Questions