- Home /
How to use ObjImporter script available on unity wiki?
I copied this (http://wiki.unity3d.com/index.php?title=ObjImporter) into a script.
It gave an error (type should be of a monobehaviour to attach a component). So i added "monobehaviour" to the class defination public class ObjImporter : MonoBehaviour{ }
I used the following script
using UnityEngine; using System.Collections; using System.IO;
public class importOBJ : MonoBehaviour {
// Use this for initialization public GameObject emptyPrefabWithMeshRenderer; public string meshPath; public GameObject spawnedPrefab; void Start () { Mesh importedMesh= GetComponent("ObjImporter").ImportFile(meshPath); spawnedPrefab=Instantiate(emptyPrefabWithMeshRenderer,transform.position,transform.rotation); spawnedPrefab.GetComponent(MeshFilter).mesh=importedMesh; } // Update is called once per frame void Update () { } }
It gives the error "UnityEngine.Component does not contain the definition for ImportFile"
Answer by Slayerkris · Jun 20, 2013 at 09:45 AM
Mesh importedMesh= GetComponent<ObjImporter>().ImportFile(meshPath);
This should make it work.
thanks a lot...:). I shall try this as well, but I have made it work by using a plugin.
Answer by akeplinger · Nov 17, 2014 at 08:59 PM
Using ObjImporter and a sampling of some OBJ test files found online I'm not seeing the uv maps after calling ImportFile()
After assigning the mesh I am calling the following: GetComponent().mesh.RecalculateBounds(); GetComponent().mesh.RecalculateNormals();
It may be the models that I'm testing with. At first the normals were incorrect, but the Recalculate commands resolved that.
When trying to use this importer my code is similar to that above:
public GameObject emptyObject; public string objPath = "C:/Users/Nick $$anonymous$$ann/Desktop/Zombie.obj"; public GameObject spawnedPrefab;
public void LoadOBJAtRuntime(){
(*)$$anonymous$$esh imported$$anonymous$$esh = GetComponent<ObjImporter> ().ImportFile (objPath);
spawnedPrefab=Instantiate(emptyObject,transform.position,transform.rotation) as GameObject;
spawnedPrefab.GetComponent<$$anonymous$$eshFilter>().mesh=imported$$anonymous$$esh;
}
I get a NullReferenceException:Object reference not set to an instance of an object on the first line of LoadOBJAtRuntime. (*)
Any ideas?
Answer by vagelis199 · Nov 07, 2020 at 11:32 PM
@Chitransh You're not supposed to use ObjImporter in Mono Behaviour that's why you get that error at the beginning, it is its own public class that could be accessed by calling ObjImporter()
//create a ObjImporter Object
ObjImporter ImporterMesh = new ObjImporter();
// create a mesh and use ObjImporter to import your obj file and make it a mesh
Mesh MyMesh = ImporterMesh.ImportFile(ObjPath);
// then asign the mesh object to a mesh filter in your scene
GameObject.GetComponent<MeshFilter>().mesh = MyMesh;
Your answer
Follow this Question
Related Questions
Unity 5.3.4f1 + Vuforia 5.5.9 = error 3 Answers
Heating Issue in AR android app 1 Answer
How to control an Vuforia AR character using the Third Person Controller in Unity? 1 Answer
Augmented Reality on a large cylindrical cloumn 0 Answers
I cant get a Video to play on an imported object from Blender 2.8 0 Answers