There is a piece of code that creates an array of THREE.Mesh objects (Three.js)

this.multipleChoice = function(n){ console.log('log'); //проверка editor.ray.setFromCamera(editor.mouse, editor.camera); editor.intersects = editor.ray.intersectObjects(editor.cubes.children); //модель в зоне мышки? if(editor.intersects.length > 0){ editor.MINTERSECTED[n] = editor.intersects[0].object; //объект присваивается опр. ячейке массива } else editor.mN--; console.log(editor.MINTERSECTED); //тест } 

But instead

Array [2]

0: THREE.Mesh (many parameters in objects are different)

1: THREE.Mesh

length: 2

...

Occurs in the console this:

Array [2]

1: THREE.Mesh

length: 2

...

(When entered into the console, editor.MINTERSECTED [0] displays undefined)

Can you help?

  • add console.log(n); - zero there, most likely, will not appear. - Igor
  • Zero is there, that's the problem, then the zero element from the array then simply disappears - nk2
  • add console.log(n); inside if - Igor

1 answer 1

The solution was rather unexpected: instead of declaring an array, I had to declare an object of type THREE.Object3D, inside of which there is an array of children []

  this.multipleChoice = function(n){ console.log('log'); //проверка editor.ray.setFromCamera(editor.mouse, editor.camera); editor.intersects = editor.ray.intersectObjects(editor.cubes.children); //модель в зоне мышки? if(editor.intersects.length > 0){ editor.MINTERSECTED.children[n] = editor.intersects[0].object; //editor.MINTERSECTED = new THREE.Object3D } else editor.mN--; console.log(editor.MINTERSECTED); //тест }