1

I'm trying to use the mesh from an .obj file.

I'm not doing a game from scratch but rather a mod for an already built game, so I only have scripts to load new things. I tried Resources.Load() but nothing was loaded. The exact code I tried is: MeshFilter meshObject = Resources.Load<MeshFilter>("Resources/meshName"); I also tried with Mesh instead of MeshFilter.

The file is in the folder "Resources" (the folder itself is where the main.cs is, inside the visual studio project), in case this is important.

Something else I tried is loading the .obj as a GameObject and then get its mesh:

GameObject meshObject = Resources.Load<GameObject>("name");
Mesh myMesh = meshObject.GetComponent<Mesh>();

This got me a null exception.

1
  • Sorry for resurrecting an old post. I just wanted to write a quick reminder for everybody seeing this: remember to instantiate the object you load from resources! i.e. GameObject instantiatedModel = Instantiate(meshObject) Commented Nov 24, 2023 at 7:15

3 Answers 3

1

Your second approach is correct. Unity considers resources in the resources folder as GameObjects. So you load in the game object of your mesh as you are, then you have to get the component. You forgot to specify the component, so this should work just fine:

GameObject meshObject = Resources.Load<GameObject>("name");
Mesh myMesh = meshObject.GetComponent<Mesh>();

If this is what you are already trying, it is an issue of your asset file that we can't help without more information on where the files is from/how it is made. Try a MeshFilter with this method as well.

Sign up to request clarification or add additional context in comments.

1 Comment

It is indeed what I tried already. The .obj file was made by me in 3ds max; The file name is SilverAssets.obj and it contains a single mesh called "Silver_ears_n_tail". The file works fine in any 3d viewer and even in a Unity project. The exact error I'm getting is ` NullReferenceExpection: System.NullReferenceException: Object reference not set to an instance of an Object` and I believe this happens when trying to use the mesh obtained.
0

As said in this answer you should probably load the asset as a GameObject.

var meshObject = Resources.Load<GameObject>("SilverObject");

Can't confirm it since only on the phone currently - actually I believe this only works for .prefab files.

However, if it works then still Mesh is no component so using GetComponent<Mesh>() won't work.

you can try rather using

var mesh = meshObject.GetComponent<MeshFilter>().mesh;

or also

var mesh = meshObject.GetComponent<MeshFilter().sharedMesh;

instead.


In general you can also use Runtime OBJ Importer in order to load any OBJ + MTL file on runtime

And also note from the official Best Practices for Resources:

Don't use it!

Comments

0

When I took GameObject from the ".obj" file like this:

var obj =  Resources.Load<GameObject>("file");

Then I noticed that in this GameObject (obj) there is only one Transform component and one child GameObject. And if we take this child GameObject:

var childObj = obj.transform.GetChild(0).gameObject;

Then we will see 3 components already: Transform, MeshFilter and MeshRenderer.

Thus, I think that anyone who has encountered a similar problem and cannot take a Mesh from an ".obj" file, this code should help:

var mesh = Resources.Load<GameObject("file").transform.GetChild(0)
                    .GetComponent<MeshFilter>().sharedMesh;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.