How to load obj file in Godot script

Godot Version

4.2.1

Question

Right now I have this code that I found on the internet

var preloaded_mesh = preload("res://levels/first/collision.obj")
var level = MeshInstance3D.new()
level.mesh = preloaded_mesh

But I get an error

Unexpected “Identifier” in class body.
it is thrown by the last line level.mesh = preloaded_mesh
tbh it isn’t very well documented so I don’t know what I am doing wrong, I am new to GODOT

I am very new to Godot too, but maybe you could try to give more context?
I have not seen an error similar to this one but maybe we could try to work around it?
However, I would think it is because level.mesh, wich you didn’t identify properly.

The error means that you are trying to write code in the class body. You need to do that inside functions. Don’t go blindly copy&pasting stuff from the internet without knowing what you are doing.

If you are a new user to the engine, it’s better to start with the Getting Started part of the documentation here Introduction — Godot Engine (stable) documentation in English it will teach you how to use the editor, the concepts of Godot, and how to structure 2 small games one in 2D and another in 3D.

1 Like

That was it thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.