- Home /
Model imported at runtime not showing in-game
Hi Unity Community
I am trying to use the .obj importer which seems to be successfully loading a .obj model at runtime as a gameObject from my local drive (signified by the debug vertex count and position returns), however the model does not seem to appear in the main camera.
I have tried to insure that the model has the necessary components and is in view from the camera, however, as the script is not throwing me any errors, I cannot seem to resolve this; any ideas?
#pragma strict
var impMesh : Mesh; //imported mesh assigned here
var impTex : Material; //imported material (missing from import code atm)
var matShader : Shader;
//var matTex : Texture;
//var matColour : Color;
var emptyObj : GameObject; //empty placeholder for mesh
var instPos : Vector3; //imported obj inst location
var meshObj : GameObject; //GameObj mesh to be assigned to
var objMesh : Mesh; //mesh of meshObj
var objTex : Material; //assigns texture to model (subs in plain material atm)
function Start ()
{
MeshImport();
}
function Update ()
{
}
function MeshImport()
{
var objImp = new ObjImporter(); //creates ObjImp class instance
var impMesh = objImp.ImportFile("/Users/...fileAddress../monkey.obj"); //assigns meshImp w/ mesh -> replace w/ button
var meshObj = new GameObject ("TestObject", typeof(MeshRenderer), typeof(MeshFilter)); //create empty gameobject
var objMesh = meshObj.GetComponent(MeshFilter).mesh; //assign mesh to var
var objTex = meshObj.GetComponent(MeshRenderer).material;
objMesh = impMesh; //assigns imported mesh to mesh of gameobject
objTex = impTex; //impTex set in Inspector atm
meshObj.transform.position = Vector3(0,0,-10);
Debug.Log(objMesh.vertexCount);
Debug.Log(meshObj.transform.position);
}
Many thanks in advance! Ryan
Answer by manutoo · Apr 15, 2015 at 03:47 PM
This is better :
var meshFilter = meshObj.GetComponent<MeshFilter>();
meshFilter.mesh = impMesh;
Else you're just assigning the mesh to a local variable.
Same for the texture.
Hi @manutoo
Thank you very much for your response, to clarify; what difference do these syntactical changes make?
i.e. the code you provided me: var meshFilter = meshObj.GetComponent();
vs the code I already had: var obj$$anonymous$$esh = meshObj.GetComponent($$anonymous$$eshFilter).mesh;
The reason I ask is that I have made these changes and received a whole heap of error messages expecting the syntax I previously had.
Oops, sorry, you're writing code in JS, and I wrote back in C# . I'd recommend you to switch to C# if you can, I think it's more commonly used amongst Unity coders, and knowing C# will also be useful outside of Unity. So for your current code, keep "meshObj.GetComponent($$anonymous$$eshRenderer)" ..! ;-)
Ha! I thought that syntax looked unfamiliar, obviously I'm still learning. Darn, I wish I had known that before I had started; probably too invested in JS to turn back now but thanks for the suggestion.
@manutoo despite the language barrier you inadvertently still helped; thanks for that!
To clarify the issue was the in the declaration of the the variables that held the .mesh and .material properties where the components needed to be referenced when assigning the back the new mesh and material properties as shown in the code changes below.
//var obj$$anonymous$$esh = meshObj.GetComponent($$anonymous$$eshFilter).mesh; //(old)
var meshFilter = meshObj.GetComponent($$anonymous$$eshFilter); //(new)
//obj$$anonymous$$esh = imp$$anonymous$$esh; //(old)
meshFilter.mesh = imp$$anonymous$$esh; //(new)
var objTex = meshObj.GetComponent($$anonymous$$eshRenderer); //(new) same process for .material
objTex.material = impTex; //(new)
I didn't "inadvertently" help, it was on purpose ! :-p What you just wrote is the JS version of my C# code just above. Your issue was with the code logic, not with the language specifics, so the solution idea is same both in C# and JS..! ;-)