Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gian_unity190 · Jan 28, 2020 at 10:15 PM · transformmeshscript.switch

change object mesh with script

I want to change the Mesh of an object in-game. I have 5 meshes and I want one object where 5 different buttons would switch to the different meshes. the mesh position stays the same. this should also change the mesh collision for the rigid body. what command can I use?

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
3
Best Answer

Answer by Ybs · Jan 29, 2020 at 12:56 PM

Depends on whether you have a MeshFIlter component or a SkinnedMeshRenderer component. Figure out which one you are using and then try this: 1. Put your blend file or other 3d file in your /Resources folder. Create one if you need to. 2. Use this script. Substitute MeshFilter with SkinnedMeshRenderer depending on which component is attached to your GameObject.

 public GameObject yourGameObject; //assign your gameobject from the inspector here
 MeshFilter yourMesh;
 
 yourMesh = yourGameObject.GetComponent<MeshFilter>();
 yourMesh.sharedMesh = Resources.Load<Mesh>("your mesh name here");


If you also want to change the material color, you can use:

 yourMesh.GetComponent<Renderer>().material.color = new Color(r,g,b,a);
Comment
Add comment · Show 6 · 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 gian_unity190 · Jan 29, 2020 at 01:27 PM 0
Share

sorry, I'm new to unity! how do I figure out if my mesh is "skinned or filtered"? also when using: your$$anonymous$$esh = yourGameObject.GetComponent(); where can I input the mesh I want to use?

the way I have to input the code is to put either one of the GameObject or Filtered object lines under public class, right? and then when changing the mesh I need to use either of the other 2 lines?

sorry if I'm wrong :D

avatar image Ybs gian_unity190 · Jan 29, 2020 at 01:48 PM 0
Share

To input your gameobject you can put "public" before "GameObject", so that it becomes visibile in the Inspector and you can drag and drop the GameObject form the unity hierarchy into the variable field of the script (make sure to attach the script to a gameobject:

 public GameObject yourGameObject;

Here's a tip, if you attach this script to the gameobject you want to change the mesh of, you don't need this variable. Just use "this.gameObject" to refer the gameobject.

 $$anonymous$$eshFilter mesh = this.gameObject.GetComponent<$$anonymous$$eshFilter>();

The first two lines in my reply should be put below the class declaration, before Start(). The other two lines should be put inside Start(). $$anonymous$$ake sure you use ALL of those lines, just change $$anonymous$$eshFilter to Skinned$$anonymous$$eshRenderer if you need to. To know this, look at the inspector while selecting your gameobject. It will probably contain one or the other.

avatar image gian_unity190 Ybs · Jan 29, 2020 at 02:14 PM 0
Share

ok so now i have a slot to input the object i want the current object to change to?

look this is the script im using right now...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class ChangePlayer : $$anonymous$$onoBehaviour
 {
     public GameObject yourGameObject;
     public $$anonymous$$aterial[] material;
     Renderer rend;
     private $$anonymous$$eshFilter your$$anonymous$$esh;

     // Start is called before the first frame update
     void Start()
     {
         $$anonymous$$eshFilter mesh = this.gameObject.GetComponent<$$anonymous$$eshFilter>();
         rend = GetComponent<Renderer>();
         rend.shared$$anonymous$$aterial = material[0];
     }
     void FixedUpdate()
     {

         if (Input.Get$$anonymous$$ey("1"))
         {
             rend.shared$$anonymous$$aterial = material[0];
             your$$anonymous$$esh = yourGameObject.GetComponent<$$anonymous$$eshFilter>();
         }
         if (Input.Get$$anonymous$$ey("2"))
         {
             rend.shared$$anonymous$$aterial = material[1];
         }
         if (Input.Get$$anonymous$$ey("3"))
         {
             rend.shared$$anonymous$$aterial = material[2];
         }
         if (Input.Get$$anonymous$$ey("4"))
         {
             rend.shared$$anonymous$$aterial = material[3];
         }
         if (Input.Get$$anonymous$$ey("5"))
         {
             rend.shared$$anonymous$$aterial = material[4];
         }

     }
 }


so "yourGameObject" is the public input slot. then under start, I say the mesh is the current game object with: $$anonymous$$eshFilter mesh = this.gameObject.GetComponent(); and then with if (Input.Get$$anonymous$$ey("1")) I detect if im pressing "1" now i use your$$anonymous$$esh = yourGameObject.GetComponent(); to change the current mesh into the selected mesh, right? but how would I do this with multiple meshes? also its not working.. like there are no errors but nothing is happening...

Show more comments
avatar image inorganik · Aug 16, 2021 at 01:47 PM 0
Share

When you are referencing the mesh with the string in the Load call, what exactly do you refer to? I have imported .fbx files that have a mesh in them and I have referred to those strings, but it isn't working. I think I must be referencing the mesh in the wrong way. Any ideas how to fix?

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

180 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 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 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 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 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 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 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 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 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

Lighting issues with script-generated polygons 1 Answer

Calculating mesh over GameObject scale 1 Answer

How can I go about moving my camera smoothly 1 Answer

What this does? Mesh.GetBlendShapeFrameVertices 1 Answer

Create Transform to part of mesh? 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