Why do I get this error: can not set property x of undefined, when trying to load an OBJ file?

I am trying to make a gun for an FPS game. I can make a shape that follows you around when you move, like you are holding a gun. Here is the code for that:

let gun = BABYLON.MeshBuilder.CreateCylinder("rl", {diameterTop: 0.8, diameterBottom: 0.9, height: 3, tessellation: 64}, scene);
gun.renderingGroupId = 1;
gun.material = new BABYLON.StandardMaterial("rlMat", scene);
gun.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
gun.rotation.x = Math.PI/2;
gun.parent = camera;
gun.position = new BABYLON.Vector3(1, -2, 5);

But that is kind of plain. So I am trying to add a OBJ gun model to make it look better.

Here is what I have so far:

let gun = BABYLON.SceneLoader.ImportMesh("gun", 
"https://dl.dropbox.com/s/4x3e5136opsm9el/", "m1911-handgun.obj", scene,);
  gun.renderingGroupId = 1;
 gun.material = new BABYLON.StandardMaterial("rlMat", scene);

gun.rotation.x = Math.PI/2;
gun.parent = camera;
gun.position = new BABYLON.Vector3(1, -2, 5);

If you try to open the URL in the browser, it won’t work because I changed it according to the docs here: Using External Assets In the Playground | Babylon.js Documentation.
But I tried the link on a different playground, so I know that it really does work.
But the gun is not there. It gives me this errorr: cannot set property of x to undefined. Please help.
Thanks for any help in advance!

Would be great if you could share a repro in the playground ???

https://playground.babylonjs.com/#NT4QZ8#84

1 Like

it is because importMesh does not return a mesh and you either need to use importMeshAsync and await it or put you gun manipulation code in the importMesh onsuccessCallback parameter.

Can you help me do that?

This will help Loading Any File Type | Babylon.js Documentation

OK I have this code:

BABYLON.SceneLoader.ImportMesh("", 
"https://dl.dropbox.com/s/4x3e5136opsm9el/", "m1911-handgun.obj", scene, 
function(newMeshes){
var gun = newMeshes[0].getChildMeshes()[0];
gun.renderingGroupId = 1;
gun.rotation.x = Math.PI/2;
gun.parent = camera;
gun.position = new BABYLON.Vector3(1, -2, 5);
});

But the gun is not in the hand.
It is on the land :confused:

You should try to repro in the playground following this: Using External Assets In the Playground | Babylon.js Documentation

This will help the community troubleshoot.

Do you have any other ideas of how to attach it to the camera so It looks like you’re holding it?

this should work and is probably the best solution, making the repro would help us addressing it in no time :slight_smile:

I am sorry I am confused. You want me to make a copy of the playground?

Here is a link to a clone of the playground that I made: Babylon.js Playground
You can edit that one.

cool the first one did not have the gun :slight_smile:

If you open the dev tools, you can directly see the issue :

You are not importing 1 but 5 meshes in this object:
https://playground.babylonjs.com/#MBLWF1#1

so you should either make all of them children of the gun or you could use a transformnode in between for simplicity:

https://playground.babylonjs.com/#MBLWF1#2

IT WORKS!!! Thanks so much! You really helped me!