Search Unity

Noob C# and Unity Question

Discussion in 'Scripting' started by chronochris, Jul 27, 2015.

  1. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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):
    1.   public Mesh ImportFile(string filePath)
    2.     {
    3.         meshStruct newMesh = createMeshStruct(filePath);
    4.         populateMeshStruct(ref newMesh);
    5.  
    6.         Vector3[] newVerts = new Vector3[newMesh.faceData.Length];
    7. //...
    8. mesh.RecalculateBounds();
    9.         mesh.Optimize();
    10.      
    11.         return mesh;
    Okay, well I need to use that mesh and attach it to an empty gameobject in this script.
    Code (CSharp):
    1.                 GameObject go = new GameObject();
    2.                 go.AddComponent<MeshRenderer>();
    3.                 go.transform.position = new Vector3(0, 0, 0);
    4.                 go.transform.rotation = this.gameObject.transform.rotation;
    I'm confused at the return "mesh" part. and how to actually attach it.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're going to need a MeshFilter component as well - and assign your mesh to that component's mesh value.
     
  3. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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):
    1.  
    2.  public ObjImport obj;
    3. ///...
    4. GameObject go = new GameObject();
    5.                 go.AddComponent<MeshFilter>();
    6.                 go.AddComponent<MeshRenderer>();
    7.  
    8.                 go.GetComponent<MeshFilter>().mesh = obj.ImportFile(FileToOpen);
    The Mesh is still null though in the hierarchy at runtime.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Does obj.ImportFile(FileToOpen) return a valid mesh? Check the return value before you assign it.
     
  5. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    @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
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  7. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    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.