Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RyanAchtenSoma · Apr 15, 2015 at 12:51 PM · importmodelruntime

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanAchtenSoma · Apr 16, 2015 at 02:36 AM 0
Share

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.

avatar image manutoo · Apr 16, 2015 at 02:44 AM 0
Share

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)" ..! ;-)

avatar image RyanAchtenSoma · Apr 16, 2015 at 02:47 AM 0
Share

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.

avatar image RyanAchtenSoma · Apr 16, 2015 at 06:28 AM 0
Share

@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) 

avatar image manutoo · Apr 16, 2015 at 06:36 AM 1
Share

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..! ;-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Material from .obj not being imported in Android App 0 Answers

Importing models from streamingassets 0 Answers

Shooting an Arrow (Blender Model issue?) 1 Answer

How to use/access animation of imported model? 1 Answer

Is it possible to make a model in a program then import it to unity make a terrain and place the thing/building on the terrain and export the whole thing BUT then import it into Skyrim? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges