Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Don't see the mesh

Discussion in 'Scripting' started by Maklaud, May 26, 2017.

  1. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    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):
    1. var normals = mesh.normals;
    2. for (int i = 0; i < normals.Length; i++)
    3. {
    4.         normals[i] = Vector3.back;
    5. }
    6. 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!
     
  2. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Maybe you "meshed" it up? hehe jokes aside.. Do you have a Mesh Render component attached?

    Flip the polygones?
     
  3. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    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):
    1. var mesh = new Mesh();
    2.  
    3.         Vector3[] vertices = new Vector3[4];
    4.         vertices[0] = new Vector2(x1, y1);
    5.         vertices[1] = new Vector2(x2, y2);
    6.         vertices[2] = new Vector2(x3, y3);
    7.         vertices[3] = new Vector2(x4, y4);
    8.  
    9.         Vector2[] uvs = new Vector2[4];
    10.         uvs[0] = new Vector2(0, 1);
    11.         uvs[1] = new Vector2(1, 1);
    12.         uvs[2] = new Vector2(0, 0);
    13.         uvs[3] = new Vector2(1, 0);
    14.  
    15.         int[] triangles = new int[6] { 0, 1, 3, 3, 2, 0 };
    16.  
    17.         mesh.vertices = vertices;
    18.         mesh.uv = uvs;
    19.         mesh.triangles = triangles;
    20.  
    21.         var normals = mesh.normals;
    22.         for (int i = 0; i < normals.Length; i++)
    23.         {
    24.             normals[i] = Vector3.back;
    25.         }
    26.         mesh.normals = normals;
    27.  
    28.         meshes.Add(mesh);
    29.  
    30.         var meshObject = Instantiate(GameManager.Instance.RoadMeshPrefab, Vector3.zero, Quaternion.identity);
    31.         meshObject.GetComponent<MeshFilter>().mesh = mesh;
    32.         meshObject.transform.parent = RoadParent.transform;
    33.         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.
     
  4. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
  5. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    Yes, it was the first version. Then Vector3.back.
     
  6. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    Vector3.forward, not front :)
     
  7. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    No more ideas?
     
  8. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Last edited: May 28, 2017
  9. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    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.
     
  10. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Did you comment out the manual normal making code?
     
  11. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
  12. Hyrtwol

    Hyrtwol

    Joined:
    Sep 29, 2015
    Posts:
    8
    try with
    Code (CSharp):
    1.  
    2. meshObject.GetComponent<MeshFilter>().sharedMesh = mesh;
    3. meshObject.GetComponent<Renderer>().sharedMaterial.color = color;
    4.  
    otherwise it will create a new instance of the mesh and material
     
  13. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Yeah I RecalculateBounds() too
     
  14. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    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!