Search Unity

Editor - System out of memory!

Discussion in 'Scripting' started by bali33, Jun 15, 2012.

  1. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Hi all,

    I have this very simple script in which I try to iterate through every MeshFilter that contains my root GameObject.

    Code (csharp):
    1.  
    2. #pragma strict
    3. import System.Collections.Generic;
    4.  
    5. @script RequireComponent(MeshFilter)
    6.  
    7. @script RequireComponent(MeshRenderer)
    8.  
    9. function Start () {
    10.    
    11.     var meshFilters = GetComponentsInChildren.<MeshFilter>();
    12.    
    13.     var length:uint = meshFilters.Length;
    14.    
    15.     for (var i:uint = 0; i < length; i++){ 
    16.          
    17.         if (meshFilters[i].mesh)
    18.             Debug.Log("mesh : " + meshFilters[i].mesh);
    19.     }
    20. }
    21.  
    My scene is about more 500 meshes for about 272.5 triangles. When I launch the scene I have a window error which displays the following message:

    After few tests I can say that the line of code wich make the editor crash is

    Code (csharp):
    1.  
    2. meshFilters[i].mesh
    3.  
    Actually is trying to access this property of too many objects that make the editor crash.

    So there is a solution to allow iterate all that meshFilter objects and be able to access to any property of them without having the editor crash ?

    Any idea ?

    Thank you guys.
     
  2. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Hi,

    So nobody has any idea ? Does that mean it's not possible to iterate through all meshes of a scenes which contains (to) many meshes and/or triangles ?

    Thank you.
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Try sharedMesh instead of mesh. Accessing mesh forces Unity to create a copy of the mesh specific to that object, which consumes memory; so your loop is actually creating 500 copies of your mesh.
     
  4. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Ok, thank you for the advice. I will test it today and let you know if it works.

    Thanks again.