Weird Parenting Error

Heya folks, wanted to know if anyone has come across the error “Uncaught TypeError: this.position.copyFrom is not a function
at t.setParent (babylon.js:16)
at t.addChild (babylon.js:16)”
while trying to parent one mesh to another using:

cubes[10].addChild(cubes[2]);

where cubes is just an array of box meshes

Welcome aboard!

It looks like cubes[2] may not be a mesh as you think it is. Try to console.log it.

If you can make a repro in the Playground it will help others help you.

Hey Evgeni. At the beginning of the scene I create cubes using a for loop like this:

  for (let i = 0; i < 27; i++) {
      cube_options = {
        width: 2,
        height: 2,
        depth: 2,
        faceColors: cubeNumFaceColors[i],
        updatable: true,
      };

      cubeNum += 1;
      // Cube One Init Mesh
      cubes[i] = BABYLON.MeshBuilder.CreateBox(
        "cube_" + cubeNum + "",
        cube_options,
        scene
      );
      cubes[i].checkCollisions = true;
      cubes[i].position = cube_positions[i];
      cubes[i].enableEdgesRendering();
      cubes[i].edgesWidth = 6;
      cubes[i].edgesColor = new BABYLON.Color4(0, 0, 0, 1);
    }

I’ll go on and console.log it now to see if what you’re saying is true about cubes[2] not being a mesh.

I’d like to repro it in the playground but my entire script is like 7000 lines of code , unless anyone is keen on me sharing privately.

It’s more probably that cube_positions[i] is not a Vector3.

1 Like

Cube Positions is an array of vector 3s

  let cube_positions = [
    new BABYLON.Vector3(2, 0, 0),
    new BABYLON.Vector3(0, 0, 0),
    new BABYLON.Vector3(-2, 0, 0),
    new BABYLON.Vector3(2, -2, 0),
    new BABYLON.Vector3(0, -2, 0),
    new BABYLON.Vector3(-2, -2, 0),
    new BABYLON.Vector3(2, -4, 0),
    new BABYLON.Vector3(0, -4, 0),
    new BABYLON.Vector3(-2, -4, 0),
    new BABYLON.Vector3(2, 0, -2),
    new BABYLON.Vector3(0, 0, -2),
    new BABYLON.Vector3(-2, 0, -2),
    new BABYLON.Vector3(2, -2, -2),
    new BABYLON.Vector3(0, -2, -2),
    new BABYLON.Vector3(-2, -2, -2),
    new BABYLON.Vector3(2, -4, -2),
    new BABYLON.Vector3(0, -4, -2),
    new BABYLON.Vector3(-2, -4, -2),
    new BABYLON.Vector3(2, 0, -4),
    new BABYLON.Vector3(0, 0, -4),
    new BABYLON.Vector3(-2, 0, -4),
    new BABYLON.Vector3(2, -2, -4),
    new BABYLON.Vector3(0, -2, -4),
    new BABYLON.Vector3(-2, -2, -4),
    new BABYLON.Vector3(2, -4, -4),
    new BABYLON.Vector3(0, -4, -4),
    new BABYLON.Vector3(-2, -4, -4),
  ];

These all positions for cubes I’m using to create a rubik’s cube

Have you tried console.log(cubes[i].position) and looked at the output?

Your error message is saying that the copyFrom method does not exist on your position property and copyFrom definitely exists on Vector3.

1 Like

This may or may not be significant but I have been caught out by something similar before.
Doing cubes[i].position = cube_positions[i] is setting a reference and is not a copy. Is anything changing the cube_positions array?
Maybe try cubes[i].position.copyFrom(cube_positions[i])

1 Like