- Home /
Why is my script-crafted mesh invisible in game-mode? (C#)
Hey guys,
I tried to create a flat mesh on my own, just like the terrain, but so i can change its vertices height in each Update()-Call (to create something wave-like).
I'm pretty sure I created the rectangles right (there are no errors anymore) and even the pivot in Scene-View moves when I hit start.
But the mesh turns out to be invisible, even in "Wireframe"-Mode, from all directions (so i guess its not the normals). Also i got a material 'Diffuse' with a Texture on it.
I have the feeling I'm missing something simple, any suggestions?
Here's the code, still in Start(), because I don't need to transform it yet. I just want it visible. Possibly the uv's?
public class Waves : MonoBehaviour {
private int width = 4;
private int maxRows = 1; //Minimum 1
private float distance = 2f;
public bool showInformation = true;
public bool showDetailed = true;
void Start() {
if(maxRows < 1)
maxRows = 1;
//Building Vertex-Grid
List<Vector3> vertices = new List<Vector3>();
for (int row = 0; row <= maxRows; row++){
if(row%2 == 0){
for (int i = 1; i <= width ; i++ ){
vertices.Add(new Vector3(i*distance, 0f, row* distance));
}
} else {
for (int i = 1; i <= width ; i++ ){
vertices.Add(new Vector3(i*distance + distance*0.5f, 0f, row* distance));
}
}
}
//Convert List<Vector3> to Vector3[]
Vector3 [] allVertices = new Vector3[vertices.Count];
for (int i = 0; i < vertices.Count; i++) {
allVertices[i] = vertices[i];
if(showDetailed)
Debug.Log(allVertices[i]);
}
if(showInformation)
Debug.Log ("There are " + allVertices.Length + " vertices known");
//Building Triangles
List<int> triangles = new List<int>();
for (int row = 0; row < maxRows; row++){
//Green Part
for (int i = 1; i < width ; i++ ){
int a = computeGreenA(row,width,i)-1;
int b = computeGreenB(row,width,a)-1;
int c = computeGreenC(row,width,a)-1;
triangles.Add (a);
triangles.Add (b);
triangles.Add (c);
}
//Orange Part
for (int i = 1; i < width ; i++ ){
int a = computeOrangeA(row,width,i)-1;
int b = computeOrangeB(row,width,a)-1;
int c = computeOrangeC(row,width,a)-1;
triangles.Add (a);
triangles.Add (b);
triangles.Add (c);
}
//The "-1" exists, because the Array goes from, say, 0-7, while the saved Vertices are numbered 1-8!
if(showInformation)
Debug.Log ("We are at row " + row + " and currently got " + triangles.Count / 3 + " Triangles");
}
//Convert List<int> to int[]
int[] allTriangles = new int[triangles.Count];
for (int i = 0; i < triangles.Count; i++) {
allTriangles[i] = triangles[i];
if(showDetailed)
Debug.Log(allTriangles[i]);
}
if(showInformation)
Debug.Log("allTriangles[] holds " + allTriangles.Length + " entries");
//Create UV
Vector2[] uv = new Vector2[allVertices.Length];
for(int i = 0; i < uv.Length; i++) {
uv[i] = new Vector2 (allVertices[i].x, allVertices[i].z);
if(showDetailed)
Debug.Log(uv[i]);
}
//Create Mesh
Mesh mesh = new Mesh();
//mesh.MarkDynamic();
mesh.vertices = allVertices;
mesh.triangles = allTriangles;
mesh.uv = uv;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
GetComponent<MeshFilter>().mesh = mesh;
}
//Green Computations
int computeGreenA(int row,int width,int i){
return row*width+i;
}
int computeGreenB(int row, int width, int a){
return a+1;
}
int computeGreenC(int row, int width, int a){
if(row%2 == 0){
return a+width;
} else {
return a+width+1;
}
}
//Orange Computations
int computeOrangeA(int row,int width,int i){
return (row+1)*width+i;
}
int computeOrangeB(int row, int width, int a){
if(row%2 == 0){
return a-width+1;
} else {
return a-width;
}
}
int computeOrangeC(int row, int width, int a){
return a+1;
}
}
I also uploaded the concept for creation of the triangles: Triangles in a field of 7 vertices wide and 4 deep: http://imageshack.us/a/img40/267/wk61.jpg Triangles in a field of 4 vertices wide and 5 deep: http://imageshack.us/a/img62/6407/zvwv.jpg The formulas i got from my 'research': http://imageshack.us/a/img96/1368/82sd.jpg
You'll need to put
using UnityEngine; using System.Collections; using System.Collections.Generic;
in front of the script to make it work. (sorry)
Answer by Rockroot · Jun 17, 2013 at 01:15 PM
Okay, remember when I said "I'm pretty sure I calculated the triangles right"? I didn't.
The devil lies in this comment: //The "-1" exists, because the Array goes from, say, 0-7, while the saved Vertices are numbered 1-8!
The thing is, that I did -1 on a (line 52 and 62), use a, but then do -1 twice again on b and c (which are already reduced, when I reduced a)
So for people who come here with the same problem: Check your triangles again, and again, and again.
Sorry for taking your time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104204im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why my Rock Meshes are invisible in the scene and visible in the game? 3 Answers
Mesh UVs won't change positions 1 Answer
invisible mesh in game mode 1 Answer
Adding lights and cameras to scenes with cSharp script 2 Answers
Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 Answers