[Unity toolkit] Game object mesh

I’m trying to access a game objects mesh from a Babylon class component (.cs)

//MyScriptComponent.cs
     [BabylonProperty]
     public GameObject swap; 

//MyScriptComponent.ts
    this.getProperty("swap", null)

But when I try and access the mesh swap.visibility = true;
image
There’s no game object properties that I have access to and I don’t know how to access or get these properties, so what’s the best way to access a defined mesh from the inspector with the toolkit? Preferably from a GameObject.

Pinging el maestro: @MackeyK24

You can’t do it like that in the classic version. It’s an Exporter … the easiest way to get a reference to a mesh in Babylon are the scene get mesh by name functions. In the new versions I support transforms and game objects by serializing the MeshID then in your script component you will have a string property with that MeshID and use scene get mesh by ID to get a reference to a separate mesh… you can do that yourself in your script component until the new version is ready

I’ll check what is serialized later today. But I think that ID in the image above is the mesh id… use that to get a reference to the mesh. That should work for now.

What mesh did you have assigned to the swap variable … powerbardcords_unplugged ???

Hey Mackey, I must not be doing it correctly:
image

and

image

mesh is null in this example when trying the suggested way.

“What mesh did you have assigned to the swap variable” it’s not a mesh it’s a Game object:

//MyScriptComponent.cs
		[BabylonProperty]
		public GameObject swap; 

Also doesn’t work with UniqueID
image

And getMeshByName:
image

If it’s not a mesh… then use one of others… get node by I’d should return anything . Or you could use get transform node… also use the inspector and look at the id… is that the right node/game object

Also I think it’s get mesh by id … not unique id… that is a number

Yes I did try that I’m getting the GameObjects ID with getProperty then the ID with .id but it’s not getting the mesh by the ID in the example:
image

image

image

Am I doing it correctly? I don’t know how I would return the node with get transform node

Hold on a sec… I gotta boot up my pewter

Ok… For a GameObject with no geometry will be a BABYLON.TransformNode

Maybe there is something wrong with getTransformNodeByID

I really use getNodeByID … that will give something weather its a mesh, light, camera or transform node.

Example:

MyTestScript.cs

/* Babylon Editor Script Component (C# UnityScript) */

using System;
using UnityEditor;
using UnityEngine;
using Unity3D2Babylon;

namespace MyProject
{
	public class MyTestScript : EditorScriptComponent
	{
        [Header("-Script Properties-")]

        [BabylonProperty]
        public string hello = "Hello World";

		[BabylonProperty]
		public GameObject swap;

		protected MyTestScript()
		{
			this.babylonClass = "PROJECT.MyTestScript";
		}
	}
}

MyTestScript.ts

/* Babylon Mesh Component Template */

module PROJECT {
    export class MyTestScript extends BABYLON.MeshComponent {
        public constructor(owner: BABYLON.AbstractMesh, scene: BABYLON.Scene, tick: boolean = true, propertyBag: any = {}) {
            super(owner, scene, tick, propertyBag);
        }

        protected ready() :void {
            // Scene execute when ready
        }

        protected start() :void {
            // Start component function

            const swap:any = this.getProperty("swap");
            console.log(swap);

            const meshid:string = swap.id;
            console.log(meshid);

            const tnode:BABYLON.TransformNode = this.scene.getNodeByID(meshid) as BABYLON.TransformNode;
            console.log(tnode);
        }

        protected update() :void {
            // Update render loop function
        }

        protected after() :void {
            // After render loop function
        }

        protected destroy() :void {
            // Destroy component function
        }
    }
}

Console Output:

So … Use getNodeByID and cast as … although its not really a cast its just typescript sugar

Let me know if that worked for you

Yo @i73 … I think there is an issue with getTransformNodeByID with Babylon 4.0.3 but it works in 4.1.x

So use getNodeByID for now

Hope that explains a bit for you :slight_smile:

Yeah I’m using Babylon.js v4.0.3 it’s still not working for me using getNodeByID or getTransformNodeByID
image

I’m going to try and update?

Also it is a game object with Geometry if that changes anything. I’m trying to swap out a mesh with another one:

        static swap(mesh: BABYLON.AbstractMesh, swap: BABYLON.AbstractMesh){
            mesh.isVisible = false; 
            swap.visibility = 1;
        }

Can you send me your scene… package up as a unitypackage … I’ll take a look at what’s going wrong…

Also … not sure the scene manager in the classic version will work with latest Babylon engine… I’ll try update the engine later this week when I get a chance

FYI … if it is a game object with geometry it is a mesh… the inspector will tell you what’s what

Unfortunately I am unable to.

if it is a game object with geometry it is a mesh
This might be true but I don’t see this happening, it’s registered as a gameObject:
image

and when I try and access any nodes from the scene:

Is there any other options I can take to get a mesh from the scene using the toolkit? I just need to make it visible.

I’ve added the two classes and other than that can you think of anything?
SwapMesh.zip (877 Bytes)

and copying your exact ts file:

image

I’m noticing yours is a type Object, how so?
image

Use the inspector and expand the Nodes… take a screen shot and let’s see what actually in your scene… I can’t really debug without the scene. The one example I sent has every thing in it…

FYI … everything in Babylon is either a mesh , a light or a camera… game objects with no geometry is a transform node.

So even though the Sphere in unity says it’s a game object… in Babylon it will be a Babylon.Mesh

I appreciate knowing this, I learned this today.

It’s not the inspector its the actual code that I’m debugging, it’s the exact same thing just not the inspector, this is the mesh that I’m getting from the Babylon mesh component from the Gameobject:
image

And this is the null value I get when I try and getNodeById():

But if you prefer the Inspector this is the log:
image

I copied your code exactly and understand what should be happening, it’s just not getting mesh or node, the gameobject is a Gameobject with a mesh component:


So this is the code with the properties:
image
This is when I try and getMeshByID():
image

When I try getMeshByName():
image

s and ss are both null in that debug.

And here is the code:

/* Babylon Mesh Component Template */

module PROJECT {
    export class SwapMesh extends BABYLON.MeshComponent {
        public constructor(owner: BABYLON.AbstractMesh, scene: BABYLON.Scene, tick: boolean = true, propertyBag: any = {}) {
            super(owner, scene, tick, propertyBag);
        }

        protected start() :void {
            const swap:BABYLON.AbstractMesh = this.getProperty("meshToSwap");
            console.log(swap);

            const meshid:string = swap.id;
            console.log(meshid);

            var s = this.scene.getMeshByID(meshid);
            var ss = this.scene.getMeshByName(swap.name);

            const tnode = this.scene.getMeshByID(meshid);
            console.log(tnode);
        }
    }
}

And the .cs:

/* Babylon Editor Script Component (C# UnityScript) */

using System;
using UnityEditor;
using UnityEngine;
using Unity3D2Babylon;

namespace MyProject
{
	public class SwapMesh : EditorScriptComponent
	{
        [BabylonProperty]
        public GameObject meshToSwap; 

		protected SwapMesh()
		{
			this.babylonClass = "PROJECT.SwapMesh";
		}
	}
}

I just realized what it was @MackeyK24 I have the object turned off in the unity inspector and because of that it’s not finding it in the scene, I had no idea:


I’m going to change the transforms to the mesh transforms when I need to invoke the swap, unless you can suggest something better.

I’m embarrassed, thank you so much for all your help…

I just saw that too… Sphere is disabled.

I made a video just to clarify things a bit…

FYI… that’s I said send me your scene so I can look at it :slightly_smiling_face:

Also… when say use or show inspector… I was talking about the Babylon inspector… it would have shown no sphere as well… the video goes into this too