Hi all! I'm creating meshes via the code, but depending on their position sometimes I can see them and sometimes not. I tried to set their normals: Code (CSharp): var normals = mesh.normals; for (int i = 0; i < normals.Length; i++) { normals[i] = Vector3.back; } mesh.normals = normals; But it didn't help. Even if I scale the scene (in Pause mode) to the mesh, it is hidden, but it exists. Thanks!
Maybe you "meshed" it up? hehe jokes aside.. Do you have a Mesh Render component attached? Flip the polygones?
Yes, renderer exists. And mesh filter. I use my prefab, create a mesh, attach it to a gameobject. Here is the whole code. Code (CSharp): var mesh = new Mesh(); Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector2(x1, y1); vertices[1] = new Vector2(x2, y2); vertices[2] = new Vector2(x3, y3); vertices[3] = new Vector2(x4, y4); Vector2[] uvs = new Vector2[4]; uvs[0] = new Vector2(0, 1); uvs[1] = new Vector2(1, 1); uvs[2] = new Vector2(0, 0); uvs[3] = new Vector2(1, 0); int[] triangles = new int[6] { 0, 1, 3, 3, 2, 0 }; mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; var normals = mesh.normals; for (int i = 0; i < normals.Length; i++) { normals[i] = Vector3.back; } mesh.normals = normals; meshes.Add(mesh); var meshObject = Instantiate(GameManager.Instance.RoadMeshPrefab, Vector3.zero, Quaternion.identity); meshObject.GetComponent<MeshFilter>().mesh = mesh; meshObject.transform.parent = RoadParent.transform; meshObject.GetComponent<Renderer>().material.color = color; Sometimes I change game parameters and meshes are created in different positions. And sometimes they are visible. I don't know the reason.
Try this: mesh.RecalculateNormals(); just before you Add it meshes.Add(mesh); I've never had any luck manually adding Normals. I just RN now. --- If you make a List of meshes to then be Combined Wait til after that to run Recalculate on the final combined mesh. https://docs.unity3d.com/ScriptReference/Mesh.RecalculateNormals.html
No, didn't help. I don't combine meshes, just create a few ones. Is there another way to create a shape of my own form? To be more exact, I just need some 4-corners, not parallelograms.
Maybe the bounds of the Mesh are off screen and thus culled by the frustum culler. Try RecalculateBounds or assign a valid Bounds to the mesh yourself.
try with Code (CSharp): meshObject.GetComponent<MeshFilter>().sharedMesh = mesh; meshObject.GetComponent<Renderer>().sharedMaterial.color = color; otherwise it will create a new instance of the mesh and material
I tried all your advice and still didn't see some meshes. Then I corrected some game params and now see them all. But maybe they will dissapear again when I change some parameters again - then will try your adice again. Thanks!