- Home /
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?
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);
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
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.
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...
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?