How to change or set the mesh of a MeshInstance using gdscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Xian

I am using godot 3.1.1 stable win64. I wanted to add a mesh to a new MeshInstance node, I made using GDScript. So I used

var plane = MeshInstance.new()
plane.mesh = PlaneMesh
add_child(plane)

This did not work. So I manually added a new MeshInstance called MeshIn2 with a plane added in, using the 3D environment tab. After that, i open my script and I did

var plane = MeshInstance.new()
plane.mesh = $MeshIn2.mesh
add_child(plane)

Here is where it gets weird. Running the code I receive no error messages and my new MeshInstance is still invisible because there was no mesh, but my MeshIn2 which was clearly there in my scene until I ran the code turned invisible as well. Being skeptical, I removed

  plane.mesh = $MeshIn2.mesh

and ran the code, my MeshIn2 became visible again in my scene. I was confused so i tinkered around wondering what happens if change the mesh of my MeshIn2 so i removed all other stuff from my script and wrote

$MeshIn2.mesh = CubeMesh

The result was MeshIn2 became invisible. Please help need.

:bust_in_silhouette: Reply From: Dlean Jeans

PlaneMesh is just a class. You need to instance it:

var plane = MeshInstance.new()
plane.mesh = PlaneMesh.new()
add_child(plane)

Thank you. It works now :slight_smile:

Xian | 2019-06-30 04:32