- Home /
How do change the mesh of a game object
I have a game-object which would 'transform' to another mesh when a button is pressed. How does I assign the new mesh with one that I have already loaded into the Unity3D project structure?
Answer by AliAzin · Sep 08, 2010 at 11:59 AM
you can change MeshFilter.mesh .
Sorry to resurrect an old thread, but I'm facing a problem with that... The prefab I am taking the mesh FRO$$anonymous$$ ends up losing the mesh. The mesh (a cube) gets applied to an object on the stage (a cylinder). At first glance, it seems to work, i.e. I can see the mesh changing. However, the cube mesh is ERASED from the cube prefab permanently - until I manually reassign a cubic mesh in the Inspector window. How can I prevent this from happening?
Here is the code I am using: using UnityEngine; using System.Collections;
public class cameraCheck : $$anonymous$$onoBehaviour {
public GameObject initialObject;
public GameObject swapObject;
$$anonymous$$esh initial$$anonymous$$esh;
$$anonymous$$esh swap$$anonymous$$esh;
GameObject theTarget;
// Use this for initialization
void Start () {
theTarget = initialObject;
initial$$anonymous$$esh = initialObject.GetComponent<$$anonymous$$eshFilter>().mesh;
swap$$anonymous$$esh = swapObject.GetComponent<$$anonymous$$eshFilter>().mesh;
theTarget.GetComponent<$$anonymous$$eshFilter>().mesh = swap$$anonymous$$esh;
}
}
$$anonymous$$ake a backup. These functions $$anonymous$$eshFilter.mesh and $$anonymous$$eshFilter.shared$$anonymous$$esh can permanently change an asset.
$$anonymous$$ake sure that it ends with a mesh loaded in and not No mesh and make sure you aren't Destroying it in the prefab.
Perhaps submit this as a new question.
yes, but in this case it's the mesh of the prefab I am copying FRO$$anonymous$$ that gets destroyed. Please note that I am not "writing" anything to that object. I am just storing it in a variable, and then I am applying the variable to the other object. Why is it that the mesh I copied gets removed? It's analogous to copying (not moving) a file and then finding out that it no longer exists in the original location.
Sorry to Necro your necro but I didn't see a satisfactory answer. Because you didn't clone it you assigned it. If you look up instantiate it is the unity equivalent of object.clone the = assigns it does not create equivalent copies. SendInTheClones.GetComponent().mesh = Instantiate(objOriginal.GetComponent().mesh); Tested it with a raw cube and turned it into a little white piggy without deleting the original