How to generate an object using gdscript

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

Okay I am not new to programming and i just came from a different game engine.
i need to spawn/generate/instantiate an object into my scene using gdscript(the moment it runs).
After viewing the 3D environment, i know object MeshInstance should contain Mesh, transfrom, visibility and so on(basic understanding of objects)
So I tried to manually spawn a plane mesh and make the code similar to its parameters:

var plane =
plane.mesh = PlaneMesh
plane.skeleton = "Root"

i received a logic error. it ran but i dont see my plane mesh also i cant seem to use

plane.transform.translation or

to set the position of my new plane mesh, even though translation should have been a node under transform.
Please help. How do i spawn an object and set its coordinates.

PS i was searching online but it talked about “Classtype” or “Add_child” or “set_name”
all of which i cant seem to use

:bust_in_silhouette: Reply From: Dlean Jeans

Transform doesn’t have a translation property but an origin property. But MeshInstance inherits from Spatial which has the translation property as a shortcut for that, so it’s:


# or for global translation

And you probably need to add the plane to the scene tree:


Maybe you’re confused because Translation is under Transform in the Inspector but you can hover your mouse over to see the real underlined property name:

Spatial Inspect

Some properties are not shown in the Inspector like the transform property itself.