Search Unity

Resolved Is my code causing a bug?

Discussion in 'Scripting' started by Meaningless-Trinket, Feb 26, 2024.

  1. Meaningless-Trinket

    Meaningless-Trinket

    Joined:
    Apr 25, 2020
    Posts:
    83
    Code (CSharp):
    1. Mesh mesh = new Mesh();
    2.  
    3. Material material = h.GetComponent<Renderer>().sharedMaterial;
    4.  
    5. Transform t = h.transform;
    6.  
    7. mesh.SetVertices(verticesout);
    8. mesh.SetTriangles(Triangles, 0, true);
    9. mesh.RecalculateNormals();
    10.  
    11. RenderParams rp = new RenderParams(material);
    12.  
    13. Matrix4x4 matrix = Matrix4x4.TRS(t.position, t.transform.rotation, t.transform.lossyScale);
    14.  
    15. Graphics.RenderMesh(rp, mesh, 0, matrix);
    16.  
    I'm making a lot of meshes and I get a error.

    Resource ID out of range in SetResource: 1082400 (max is 1048575)

    Unity is slow to quit.

    I don't know if I am doing something incorrectly.
     
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    Do you destroy the meshes after you don't need them anymore?
     
  3. dougfight

    dougfight

    Joined:
    Nov 21, 2016
    Posts:
    5
    hello,

    First: make sure you don't create an infinite loop creation, always have a way out.

    Second: The number of instances or meshes created affect the game's playback performance, resulting in mild or severe freezes. I recommend doing gradual parts or with coroutines.

    Third: Do a test without attributing the materials, as this can reveal whether the poor performance or error is linked to the manipulation of many materials and textures and with this it may be thought that you could use the same shared material in other items that use the same type, to reduce creation processes and items in memory;

    Fourth: Whenever you create something, especially multiple ones, always store it somewhere so you can manipulate it. That said, after creating a certain amount of instantiations, it is recommended to destroy the inactive ones or those that are far away, in the case if you play the game with instantiation according to saved data.

    I hope I have shed some light on ideas.
     
  4. Meaningless-Trinket

    Meaningless-Trinket

    Joined:
    Apr 25, 2020
    Posts:
    83
    I was not destroying the meshes I made.

    Now I've made every mesh I could possibly need in a list and increment it after each mesh is referenced.

    Then I mesh clear.

    Thanks.
     
    bugfinders likes this.