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 wildikon · Mar 23, 2014 at 06:19 AM · meshruntimechange

change the mesh of object during runtime using javascript

Hi

how do you change a mesh of an object with javascript during runtime...

it should be as easy as gameObject.Find("myMesh").Mesh="AnotherMesh"; or gameObject.Find("myMesh").MeshFilter.Mesh="AnotherMesh"; or gameObject.Find("myMesh").Renderer.Mesh="AnotherMesh"; you know, something along these lines...

...but its not (by far its not) Every single 'answer' I have seen has not worked? I have tried over 30 of them!

I feel should be astonishingly easy as say hiding an object like...

gameObject.Find("myMesh").Renderer.enabled=false...

I want to do this with javascript not c#

any pointers would be great, thanks community!

Wildikon

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
0

Answer by robertbu · Mar 23, 2014 at 06:23 AM

Here is a bit of example code:

  #pragma strict
 
  var newMesh : Mesh;
 
  function Start() {
      var mf : MeshFilter = GameObject.Find("myMesh").GetComponent(MeshFilter);
      if (mf != null) {
          mf.mesh = newMesh;
      }
  }

Note this code assumes that "myMesh" exists and that you are changing and existing mesh. It will fail if "myMesh" does not have a MeshFilter component. It also assumes that your are initializing 'newMesh' by dragging and dropping a mesh onto this variable in the inspector.

Comment
Add comment · Show 1 · 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 wildikon · Mar 24, 2014 at 05:38 AM 0
Share

Thanks so much, your answer has helped very much!

Here is my scenario, I have a mesh (an fbx I imported made with blender3D) called it original$$anonymous$$esh and dragged into the scene. I put your script onto it (I called it $$anonymous$$eshChange.js), then next to the variable mesh1 I clicked the little circle with black dot and added another mesh (again an fbx) from my meshes folder, and a different mesh for mesh2!

I ran unity and it worked! The original mesh now swaps between mesh1 and mesh2!!

I can do a great deal with this now... Here is my $$anonymous$$eshChange.js script!

 #pragma strict
  
 var mesh1 : $$anonymous$$esh;    
 var mesh2: $$anonymous$$esh;
 
 var mf : $$anonymous$$eshFilter;
 var ch:int=0;
  
 function Start() 
 {
     mf = GameObject.Find("original$$anonymous$$esh").GetComponent($$anonymous$$eshFilter);
     
     if (mf != null)
     {
         mf.mesh = mesh1;
     }
     
     change$$anonymous$$esh(ch);
 }
 
 
 //loop between the two meshes - this is just for testing purposes, to see the mesh change in real time!
 function change$$anonymous$$esh(ch)
 {
     Debug.Log("mf="+mf);
     
     if (ch == 0) 
     {
         mf.mesh = mesh2;
         ch=1;
     }
     else if (ch == 1) 
     {
         mf.mesh = mesh1;
         ch=0;
     }
     
     yield WaitForSeconds(2);
     
     changeAgain(ch);
 }
 
 //have to add this beacuse you can't call the a function from within that function - Unity starts throwing its toys out the pram if you ask it to do this! (any ideas on this btw)
 //anyway, doing this gets my test done at least, so I live with it!
 function changeAgain(ch)
 {
     change$$anonymous$$esh(ch);
 }


thanks again for taking the trouble to answer!

WildIkon

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to dynamically change the shape of a mesh during runtime? 1 Answer

Merge game objects during game? 0 Answers

Creating meshes in Unity using Javascript 0 Answers

My runtime mesh is being created to the far right of my unit? 0 Answers

LoadImageIntoTexture Runtime Change 1 Answer


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