Error "Uncaught TypeError: scene.getUniqueId is not a function" when calling function "BABYLON.MeshBuilder.CreateLineSystem()"

When I execute my project, the following error is thrown:

Uncaught TypeError: scene.getUniqueId is not a function

This is my code (I’ve omitted the irrelevant parts):

scene = createScene(canvas);

loadData();

function loadData() {
	[...]
    model = new TreeModel(scene, ball, data);
}

function TreeModel(scene, ball, data) {
	[...]
    var lineSystem = me.lineSystem = BABYLON.MeshBuilder.CreateLineSystem(
        "lineSystem2", 
        {lines: me.lines, updatable:true},
        scene);
}

The following error is thrown (at the line where CreateLineSystem() is invoked):

Uncaught TypeError: scene.getUniqueId is not a function

It is possible that this error is caused by the fact that I’m updating the code for enabling support for WebXR. The previous code had the following import statement:

<script src="./babylon.custom.js"></script>

which I’ve replaced with the following statement:

<script src="https://preview.babylonjs.com/babylon.js"></script>

in order to enable the support for WebXR (the previous library was old and didn’t support it).

What can be the cause of the Uncaught TypeError: scene.getUniqueId is not a function error? Maybe the scene object is changed? I’ve noticed that with the old Babylon.js library the scene object was of type [object Object], while in the new library the object is of type [object Promise].

1 Like

your createScene is returning a promise so you have to use eityher async/await or call then (on the promise)

can you share your createScene code?

1 Like

Here’s the code:

var createScene = async function (renderCanvas) {
    [...]
    var scene = new BABYLON.Scene(engine);
    [...]
    const xr = await scene.createDefaultXRExperienceAsync();
    return scene;
}

I think that the invocation of BABYLON.MeshBuilder.CreateLineSystem() now is incorrect, because I cannot pass a Promise where a Scene object is expected. Is it correct? But, now that I have a Promise, how can I call BABYLON.MeshBuilder.CreateLineSystem() and pass the scene object to it?

You are using an async function so your code should be:

var scene = await createScene()
1 Like

I am having the same error: “VM616:1284 TypeError: this._scene.getUniqueId is not a function” when I create a sphere in my renderloop.

If I switch my “var scene = to await createScene()” it kind of just crashes my whole program.

This must be something simple I don’t understand?

It all depends on the code architecture. you will need to share a bit more code for us to be able to help :slight_smile:

2 Likes

OK, so I am trying to create VR multiplayer. I’ve got things set up in websockets so that I get an array of all the players and all of their positions (constantly updated). I’m trying to draw (and update) a sphere where every player is. My code is here: babylonboidsvrsockets . Raanan, I based my code on this example (which I think is yours?): Babylon.js Playground . I mostly removed aspects of your example. What I added was some loaders and some 3D boids. I think there’s something simple that I’m not understanding. The line that’s giving me problems is line 1410 in my code: " var playerBody = new BABYLON.MeshBuilder.CreateSphere(“playerBody”, {}, scene);" I wonder if creating meshes in the render/update loop is a horribly bad idea, but I need to dynamically resize the array depending on the amount of players coming and going…

I took a deep dive into sockets with a friend and I think I am approaching rendering the wrong way. I am re-engineering my approach to rendering when a new player shows up. I think that will solve this problem. I think that create a new mesh every update loop is incorrect.

In your code we find the following lines:

var createScene = async function() {
    var scene = new BABYLON.Scene(engine);
    ...
}
...
scene = createScene();
scene.then(returnedScene => {
    sceneToRender = returnedScene;
});

So the scene variable receives two values, the one returned by new BABYLON.Scene(engine) and the one returned by createScene(). The first value is an object of class Scene, while the second is a Promise. We need both values, so we have to assign those values to different variables, scene and scenePromise, and we have to move the declaration of scene outside the createScene function:

var scene = null;
var createScene = async function() {
    scene = new BABYLON.Scene(engine);
    ...
}
...
var scenePromise = createScene();
scenePromise.then(returnedScene => {
    sceneToRender = returnedScene;
});

The error this._scene.getUniqueId is not a function was due to the fact that the value of scene that we passed to the method CreateSphere was a Promise object, while this method requires a Scene object.

1 Like

Hi there, I re-architected the code and now everything works correctly. I now have a multiplayer VR example with web sockets @RaananW ! There’s complex 3D movement and everything is showing whenever anyone logs in. I had to re-architect when to create a mesh and when to destroy it, etc.

2 Likes

awesome! Can’t wait to try it out :slight_smile: