- Home /
Scale and save prefab
Hi
I'm trying to scale a mesh and then save the GameObject as a prefab. The scaling works fine but after saving the prefab the prefab's MeshFilter is always empty.
Testing steps are:
Create a Cube (GameObject > 3D Object > Cube)
Attach the Script to the Cube
Play
After this i can see the Cube is bigger and the prefab is created but when i check the prefab it's MeshFilter is empty.
public class ScaleMesh : MonoBehaviour {
public float XScale = 3.0F;
public float YScale = 3.0F;
public float ZScale = 3.0F;
void Start()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh = ResizeMesh(mesh);
PrefabUtility.CreatePrefab("Assets/" + gameObject.name + ".prefab", gameObject, ReplacePrefabOptions.ConnectToPrefab);
AssetDatabase.Refresh();
}
Mesh ResizeMesh(Mesh mesh)
{
var baseVertices = mesh.vertices;
var vertices = new Vector3[baseVertices.Length];
for (var i = 0; i < vertices.Length; i++)
{
var vertex = baseVertices[i];
vertex.x = vertex.x * XScale;
vertex.y = vertex.y * YScale;
vertex.z = vertex.z * ZScale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613180813im_/https://answers.unity.com/themes/thub/images/avi.jpg)