change mesh through C#

Hi,

i have a gameobject with a MeshRenderer and a MeshFilter.
im trying to change the mesh through C#.

public class MeshRenderGameObject : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        MeshFilter t = gameObject.GetComponent<MeshFilter>();
        t.mesh = (Mesh)Resources.Load("Assets/Assets/Teapot.FBX", typeof(Mesh));        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

object https://i.imgur.com/yWRGJ9s.png

Make sure the resource you are trying to load is in the “Assets/Resources” folder and remove “.FBX” to the string.

Make sure the object you want to load is actually located in a Resources folder. The actual path you have to specify is always relative to the Resources folder. Also keep in mind that you have to omit the file extension. You’re not dealing with files here but with serialized, compiled and packed assets.

So if your loading code looks like this:

t.mesh = Resources.Load<Mesh>("Assets/Assets/Teapot");

The actual file has to be located here for example:

"Assets/Resources/Assets/Assets/Teapot.FBX"

For more details, just have a look at the documentation of Resources.Load

Keep in mind that you have have multiple Resources folder somewhere in your Assets folder. Though the load path is always relative to the Resources folder. Also keep in mind that you could have ambiguous asset names if you have two assets with the same name in different resources folders. It’s generally adviced to avoid too many nested / buried resources folders. Those are mainly useful for third party asset packs. Though hopefully they used an actual subfolder. For example:

"Assets/SomeThirdPartyAsset/Resources/SomeThirdParty/someasset"

so it doesn’t collide with your own resources.