Am I not understanding how `scene.getMeshByName()` works?

I couldn’t find a share link in the new playground so embedded. I have a nested scene graph but have meshes with unique names. Want to grab the mesh without knowing where it is in the graph. I thought scene api pulls any mesh anywhere in the scene. I’m a missing something or is this a bug.

class Playground { 
    public static CreateScene(engine: BABYLON.Engine, canvas: HTMLCanvasElement): BABYLON.Scene {
       // This creates a basic Babylon Scene object (non-mesh)
    var scene = new BABYLON.Scene(engine);

    // This creates and positions a free camera (non-mesh)
    var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);

    // This targets the camera to scene origin

    // This attaches the camera to the canvas
    camera.attachControl(canvas, true);

    // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
    var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);

    // Default intensity is 1. Let's dim the light a small amount
    light.intensity = 0.7;

    // Our built-in 'sphere' shape.
    var sphereName = "sphere"
    var sphere = BABYLON.MeshBuilder.CreateSphere("sphereName", { diameter: 2, segments: 32 }, scene);

    var foo = new BABYLON.TransformNode("foo", scene)
    var bar = new BABYLON.TransformNode("bar", scene)

    var okay = bar.getChildren()[0]
    var notOkay = scene.getMeshByName(sphereName)
    console.log(`Okay? ${} Why are you empty? ${notOkay}`)

    // Move the sphere upward 1/2 its height
    sphere.position.y = 1;

    // Our built-in 'ground' shape.
    var ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 6, height: 6 }, scene);

    return scene;

Hi @delaneyj
You are mixing a variable and value together,

First you create a variable, sphereName, with the string value “sphere”.

var sphereName = “sphere

But then you use “sphereName” as a new string, making the name “sphereName” and not “sphere”

var sphere = BABYLON.MeshBuilder.CreateSphere(“sphereName”, { diameter: 2, segments: 32 }, scene);

So when you try to find a mesh with the name “sphere” as referenced by the variable, sphereName.
it’ll return null,

var notOkay = scene.getMeshByName(sphereName)

since the actual name is still “sphereName”

scene.getMeshByName(“sphereName”); // would work in this case.


Thanks, yes is setup the playground incorrectly. Found the root issue in my case was not awaiting an async loading of meshes, causing the query to happen before the load was complete. Thanks for checking though!