Problem with loading meshes at runtime!

Hello reader!

I have a problem with loading meshes at runtime!
I’m trying to instantiate an object through a class.

Here is my code where I load the mesh itself:

Mesh mesh = (Mesh)Resources.Load("Assets/Models/cubemesh", typeof(Mesh));

And the assign code:
mesh is the input to this function

public void InstantiateObject(Mesh mesh)
    {
        Debug.Log(mesh);
        if(mesh != null)
        {
            Debug.Log("Mesh exists");
        }
        else
        {
            Debug.Log("Mesh does not exist");
        }
        //Create NPC object
        npcObject = new GameObject(name);
        MeshFilter meshFilter = npcObject.AddComponent<MeshFilter>();
        meshFilter.mesh = mesh;
       
    }

This code it’s output is: “Mesh does not exist”. But I clearly input a mesh. Please help me!

Have you tried to load the resource directly as gameobject? Or save the mesh as prefab and test it again.
Unity - Scripting API: Resources.Load maybe that links helps, too

If you want to use Resources.Load, the asset needs to be in a folder called “Resources”, and you call Load using only the path from within that Resources folder, for example “Models/cubemesh”.

Thank you, makeshiftwings. I changed around the path of the object. It is now linked to the resources folder.

@SlyRipper
The mesh is already saved as an prefab. As I try to import the mesh as an gameobject I get this error:
NullReferenceExeption: Object reference not set to an instance of an object.

Here is my code:

private Mesh mesh;

GameObject meshObject = (GameObject)Resources.Load("Models/cubemesh", typeof(GameObject));
        mesh = meshObject.GetComponent<MeshFilter>().mesh; //The IDE says that the error is located right here

InstantiateObject(mesh);

public void InstantiateObject(Mesh mesh)
    {

        //Create NPC object
        npcObject = new GameObject(name);
        MeshFilter meshFilter = this.npcObject.AddComponent<MeshFilter>();
        meshFilter.mesh = mesh;
    }

This is my code.

Like I said, you don’t put the whole path in the call to Load, just the part after Resources. So change it to:

Resources.Load("Models/cubemesh",

I suggest reading the doc page that SlyRipper linked: Unity - Scripting API: Resources.Load

I read the document. I already figured that part out. And loading it as an gameobject. But I got a new problem now. I get a nullreferenceexecption.

Like makeshiftwings mentioned… Do you have an “Resources” folder? Is there a “Models” folder inside of it? outside the Resources folder the method is not usable.

I got it. Yes, I already had an resources folder because all the YT videos I watched and the document on the reference told me I had to use a resource folder.