Replacing a cube mesh object by a sphere mesh object.

Hi, I am new to Unity and a beginner in coding as well.

I searched and found some examples on this forum but could not figure it out yet, so here is what I am trying to accomplish:

I have created 2 unwrapped but untextured models in XSI and exported them to fbx files with the result:
assets/cube and its mesh under assets/cube/cubemesh
assets/sphere and its mesh under assets/sphere/spheremesh

Next I place the cube in the world and attach a script to it that replaces the cube by the sphere.
I found this on the forum:

var meshFilter : MeshFilter = gameObject.GetComponent(MeshFilter); 
if (meshFilter) 
{
meshFilter.mesh = newmesh; 
}

If I understand this right, meshFilter.mesh contains the cube’s cubemesh and replaces it by newmesh. But since newmesh means nothing I get a script error.
So I need to know how to tell newmesh is the sphere’s spheremesh?

I suppose I can create the sphere model in my scene and tell the script to grab its mesh and use it on my cube, but I asume there is a way to take it straight from my project assets without the need of creating a hidden sphere somewhere in my scene?

Thanks.

Hello,
What you need to do is define an instance variable called newMesh, and then drag the sphere mesh from the file browser onto the newMesh variable(in the inspector).

var newMesh : Mesh; //drop the sphere mesh onto this variable in the inspector

var meshFilter : MeshFilter = gameObject.GetComponent(MeshFilter); 
if (meshFilter) 
{ 
     meshFilter.mesh = newmesh; 
}

Works perfect,
thank you,
So there is no way to hard code it in?
I can make a prefab once I have set it up but just wondering :slight_smile: