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

Mesh not recalculating

Discussion in 'Scripting' started by Josenifftodd, Apr 6, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I'm trying to make my mesh recalculate but it's not working, I call it after the planet has it's inputs done so it has mountains etc. It shows it in my mesh collider, but when i click the object the mesh doesn't appear and my sphere goes straight through it.


    Code (CSharp):
    1. GameObject obj = new GameObject();
    2.             obj.name = "Ethereal Planet";
    3.             obj.tag = "Planet";
    4.             obj.transform.parent = this.transform;
    5.             obj.AddComponent<MeshFilter>();
    6.             CPlanet p = obj.AddComponent<CPlanet>();
    7.             Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
    8.             mesh.RecalculateBounds();
    9.             mesh.RecalculateNormals();
    10.             obj.AddComponent<MeshCollider>();
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I'm not apparently lol see I was wondering why when I clicked the mesh inside the inspector it said it had no verts and tris HAHAHAHAHAHA.
     
  5. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    So do I add my meshcollider first? I've read the reference script and I don't understand it fully. How can I call a mesh if the object doesn't have a mesh. So would I add a Mesh Collider than call Mesh Filter then recalculate everything?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Depends. Your code is the equivalent of this.

    Code (CSharp):
    1. Mesh myMesh = new Mesh();
    2. GetComponent<MeshFilter>().mesh = myMesh;
    If that's your intent then simply set the vertices and triangles and be done.

    If your intent was to use an existing asset as a mesh then you will need to approach it differently.
     
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Yeah I figured it out last night after messing about with the code, ermmm the only problem I have is that my planet has a LOD setting which if your far away it''ll be on 0 LOD then if your close it'll render the planet around you out but anything within a distance of 5 renders hilltops etc. I also have a Rebuild script that rebuilds the planet if it's altered in anyway through the inspector - which would you think i'd add it to? id say rebuild as then when the LOD kicks in it'll recalculate the mesh so I'm still able to stand on the planet.