- Home /
Modifying a Mesh at Runtime
I have read the documentation, and used their code (http://docs.unity3d.com/ScriptReference/Mesh.html).
I have an empty game object with a mesh filter and a mesh renderer. When I run it, I get a pink square.
I have attached this script to this game object:
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
void Update() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
When I use these settings, my pink square vanishes (instead of changing):
I get the warning "Shader wants texture coordinates, but the mesh Quad Instance doesn't have them. I have tried with other meshes, and it is still the same error. (No mesh just draws nothing from the beginning.)
The code is pulled directly from the documentation. Where am I going wrong?
Answer by zharik86 · Aug 24, 2014 at 07:02 PM
Any mesh shall have set of vertices and a set of texture coordinates(UV), and, of cpuse, triangles. That, set array of UV(newUV), for your case this array contains elements(Vector2):
(0, 0), (1, 0), (0, 1)
Or see my simple script below(for GameObject with only Transform component):
public Material myMat = null;
void Start() {
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)};
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
mesh.triangles = new int[] {0, 1, 2};
this.renderer.material = myMat; //apply material for mesh
}
I hope that it will help you.
This does help, thank you. But it does not make sense to me. Why does your code work, but this code does not work? (I changed the middle Vector):
mesh.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0) };
mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1) };
mesh.triangles = new int[] { 0, 1, 2 };
@$$anonymous$$ What not work in your code? What you see on screen? It has to be it seems true, but don't forget about normals.
@zharik86 nothing on my screen. Your code draws a pink triangle. Just by changing the 2 numbers in mesh.vertices and mesh.uv, it makes it so that nothing happens.
That is the only change. (I am in 2d mode if it is relevant).
@$$anonymous$$ Of couse, in my code you see pink triangle. To see a texture or color, you shall apply a shader. Use standard Diffuse. Create a material and attach it to a script. I will correct a script in the response for a material. In your case when you change the second coordinate if I am not mistaken, normals shall be displayed on the contrary. Therefore in the Editor try to turn the camera on 180 degrees to see back part of a triangle.
Your answer
Follow this Question
Related Questions
How to fix a object intersection problem? 0 Answers
Runtime generated mesh is not shaded right,only grey? 1 Answer
Have part of mesh change color 0 Answers
Dymanic Mesh Hiding 2 Answers
2D Sprite Shader Acting like Light 0 Answers