title: Importing an .obj model into Unity3D
body: <p>I've found a couple other posts with similar questions and also a code that should work( <a href="https://stackoverflow.com/questions/31586186/loading-a-obj-into-unity-at-runtime">Loading a .OBJ into Unity at runtime</a>) using the ObjLoader Class( <a href="http://wiki.unity3d.com/index.php?title=ObjImporter" rel="nofollow noreferrer">http://wiki.unity3d.com/index.php?title=ObjImporter</a>)</p>
  
  <p>Unfortunately that didn't work for me since I actually receive an error, which others don't.</p>
  
  <p>I get "IndexOutOfRangeException", which would normally be a simple beginner mistake using arrays, but since the code seems to work for others, I guess it is misleading and the problem lies deeper. </p>
  
  <p>I guess the problem is, that I want to attach the mesh to an actual Game Object, but I can't figure out, how to do this properly.</p>
  
  <p>Any suggestions appreciated.</p>
  
  <p>My Code</p>
  
  <pre><code>        // 1. Create an controller (this will be the pivot point later)
          GameObject controller = new GameObject(bo.objectName + " Controller");
  
          // 2. Create an primitive object, and set it as child
          GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
          sphere.transform.position = new Vector3(0, 0, 0);
          sphere.transform.SetParent(controller.transform);
          sphere.name = bo.objectName;
  
          // 3. Give it a custom mesh
          Mesh holderMesh = sphere.GetComponent&lt;Mesh&gt;();
          ObjImporter newMesh = new ObjImporter();
          holderMesh = newMesh.ImportFile(bo.modelPath);//"C:/Users/Fuby/Desktop/Test Object.obj"
          sphere.GetComponent&lt;MeshFilter&gt;().mesh = holderMesh;
  </code></pre>
  
  <p>ObjImporter Code </p>
  
  <pre><code>List&lt;int&gt; intArray = new List&lt;int&gt;();
  
  while (j &lt; brokenString.Length &amp;&amp; ("" + brokenString[j]).Length &gt; 0)
  {
    Vector3 temp = new Vector3();
    brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3); //Separate the face into individual components (vert, uv, normal)
    temp.x = System.Convert.ToInt32(brokenBrokenString[0]);
    if (brokenBrokenString.Length &gt; 1)                                  //Some .obj files skip UV and normal
    {
      if (brokenBrokenString[1] != "")                                    //Some .obj files skip the uv and not the normal
      {
        temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
      }
      temp.z = System.Convert.ToInt32(brokenBrokenString[2]); //IndexOutOfRangeException
    }
    j++;
  
    mesh.faceData[f2] = temp;
    intArray.Add(f2);
    f2++;
  }
  </code></pre>