I have this Mehod that calls code that creates a new Mesh and populates with vertices etc. I need to instantiate this mesh during runtime... so here is the code. Code (CSharp): public Mesh ImportFile(string filePath) { meshStruct newMesh = createMeshStruct(filePath); populateMeshStruct(ref newMesh); Vector3[] newVerts = new Vector3[newMesh.faceData.Length]; //... mesh.RecalculateBounds(); mesh.Optimize(); return mesh; Okay, well I need to use that mesh and attach it to an empty gameobject in this script. Code (CSharp): GameObject go = new GameObject(); go.AddComponent<MeshRenderer>(); go.transform.position = new Vector3(0, 0, 0); go.transform.rotation = this.gameObject.transform.rotation; I'm confused at the return "mesh" part. and how to actually attach it.
You're going to need a MeshFilter component as well - and assign your mesh to that component's mesh value.
There is a series of code that creates an empty mesh, and fills it. I called the initialization Method ImportFile(string filePath) like so Code (CSharp): public ObjImport obj; ///... GameObject go = new GameObject(); go.AddComponent<MeshFilter>(); go.AddComponent<MeshRenderer>(); go.GetComponent<MeshFilter>().mesh = obj.ImportFile(FileToOpen); The Mesh is still null though in the hierarchy at runtime.
@GroZZleR It returned a valid Mesh. I decided to name the newly created Mesh. So there definitely was a Mesh attached, because the name showed up in the inspector. I think the issue is is that this Object I'm importing has a few child objects each with their own mesh, and their own Mesh Renderer. I need this to be very dynamic so that the code can build the new object in runtime. I'm not sure if that's the problem, but it appears to be. This is the site where I got the ObjImport code. http://wiki.unity3d.com/index.php?title=ObjImporter So obviously there will need to be some Loops through each Child Object, each for the Mesh, and Materials. Building out collections in the code. Do you think this is possible? And thanks for your help
Do you assign a material to the MeshRenderer? That's my last guess. I'm afraid I can't be of much further help if that doesn't work. I've never worked with obj files before and have no idea how they work.
I tried using a .obj file but it had too many vertices and threw an error. So I used a .3ds that doesnt have many.