- Home /
Question by
aakwewaanaqa · Apr 02, 2017 at 10:01 AM ·
instantiatemesh
How to create a new GameObject and assign a Mesh that is just being created in a same function to it?
The problem is the mesh assign doesn't work, I don't know if it is that I use sharedmesh, but the system said if using mesh will cause a mesh leak.
public void To_CreateLand(float mindistance)
{
Vector3[] createvertices = new Vector3[widthnumber * lengthnumber];
int[] createtriangles = new int[(widthnumber - 1) * 2 * (lengthnumber - 1) * 3];
Vector3[] createnormals = new Vector3[widthnumber * lengthnumber];
Vector2[] createuvs = new Vector2[widthnumber * lengthnumber];
/// /// After Many Calculations /// ///
GameObject newland = new GameObject();
newland.name = "land " + triangle1index.ToString() + "and" + triangle2index.ToString().Substring(triangle2index.ToString().Length - 1);
newland.transform.SetParent(gameObject.transform);
newland.transform.localPosition = new Vector3(0, 0, 0);
newland.transform.localScale = new Vector3(1 / transform.localScale.x, 1 / transform.localScale.y, 1 / transform.localScale.z);
newland.AddComponent<MeshFilter>();
newland.AddComponent<MeshRenderer>();
newland.AddComponent<MeshCollider>();
newland.AddComponent<VertexAgent>();
if UNITY_EDITOR
Mesh landmesh = newland.GetComponent<MeshFilter>().sharedMesh;
landmesh = new Mesh();
landmesh.Clear();
landmesh.vertices = createvertices;
landmesh.triangles = createtriangles;
landmesh.normals = createnormals;
landmesh.uv = createuvs;
landmesh.RecalculateNormals();
landmesh.RecalculateBounds();
Mesh save = (Mesh)Instantiate(newland.GetComponent<MeshFilter>().sharedMesh); ///Error here///
save.name = newland.name;
AssetDatabase.CreateAsset(save, "Assets/System/planetmesh/" + newland.name + ".asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
endif
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612114346im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Meshes as Variables... 4 Answers
Replace GameObject vs. replacing mesh and material? 2 Answers
Instantiate hi-poly mesh takes about 3 seconds… correct? 1 Answer
How would I instantiate objects on a mesh? 2 Answers
mismatching numbers of verticles ? 0 Answers