How do I take an empty GameObject and, through C# script, load the mesh, mesh renderer, mesh filter, and material from an fbx file imported from blender? For current purposes I want to avoid drag and drop as much as possible. Thanks!
Here is something I tried but didn’t work: I have an fbx file imported and put in a Resources folder, so the mesh and material should be available to a Resources.Load call. I attached a script with the following code in the Start method to the main camera:
Mesh mesh = Resources.Load("Atile") as Mesh;
tile = new GameObject("Tile");
tile.AddComponent<MeshFilter>();
tile.AddComponent<MeshRenderer>();
tile.GetComponent<MeshFilter>().mesh = mesh;
tile.GetComponent<MeshRenderer>().material = Resources.Load("Materials/ATile-Material") as Material;
Running this gives me a GO called Tile that has a MeshFilter and a MeshRenderer, and the MeshRenderer has ATile-Material in the right spot, but there is no mesh in the mesh filter.
A better approach could be to load the FBX directly which will create the GameObject, with the same structure as if you were to drag&drop the fbx file into the scene. Just remember this things when loading resources:
- paths are relative to the project folder, except if they are located in a Resources folder.
- paths should use slashes as path separator → / even on windows
- paths are case sensitive
-
some load functions require the extension of the file being loaded (
Resources.LoadAssetAtPath
requires it,Resources.Load
does not)