Search Unity

SetNeighbour() Issues

Discussion in 'Scripting' started by reklass, Jan 23, 2014.

  1. reklass

    reklass

    Joined:
    Jan 23, 2014
    Posts:
    4
    I'm currently attempting to set a grid of neighboring terrain's LOD to match, and having serious troubles. I don't usually ask for help, but here goes. When I run the following code, Unity completely crashes with an Access Violation (0xc0000005):

    Code (csharp):
    1.         // set neighbours, find for painting
    2.         //setup raycast values
    3.         for (int a = 0; a < 5; a++)
    4.         {
    5.             Terrain[] terrainNeighbours = new Terrain[5];
    6.             RaycastHit[] neighbourRays = new RaycastHit[5];
    7.  
    8.             float xShift = 250;
    9.             float zShift = 250;
    10.             //shift loop position
    11.             if(a == 0)
    12.             {
    13.                 zShift = -250;
    14.             }
    15.             if(a == 1)
    16.             {
    17.                 xShift = 750;
    18.             }
    19.             if(a == 2)
    20.             {
    21.                 zShift = 750;
    22.             }
    23.             if(a == 3)
    24.             {
    25.                 xShift = -250;
    26.             }
    27.             Vector3 neighbourBasePosition = new Vector3(modifiedTerrain.transform.position.x + xShift,modifiedTerrain.transform.position.y,modifiedTerrain.transform.position.z + zShift);
    28.             //setup raycast values
    29.             int layerMaskPrimary = 1 << 8;
    30.             int layerMaskSecondary = 1 << 9;
    31.             int layerMask = layerMaskPrimary | layerMaskSecondary;
    32.  
    33.             Physics.Raycast(new Vector3(neighbourBasePosition.x,50000,neighbourBasePosition.z), Vector3.down, out neighbourRays[4], 100000, layerMask);
    34.             Physics.Raycast(new Vector3(neighbourBasePosition.x - 500,50000,neighbourBasePosition.z), Vector3.down, out neighbourRays[0], 100000, layerMask);
    35.             Physics.Raycast(new Vector3(neighbourBasePosition.x,50000,neighbourBasePosition.z + 500), Vector3.down, out neighbourRays[1], 100000, layerMask);
    36.             Physics.Raycast(new Vector3(neighbourBasePosition.x + 500,50000,neighbourBasePosition.z), Vector3.down, out neighbourRays[2], 100000, layerMask);
    37.             Physics.Raycast(new Vector3(neighbourBasePosition.x,50000,neighbourBasePosition.z - 500), Vector3.down, out neighbourRays[3], 100000, layerMask);
    38.  
    39.             for(int n = 0; n < neighbourRays.Length; n++)
    40.             {
    41.                 if(neighbourRays[n].collider)
    42.                 {
    43.                     terrainNeighbours[n] = neighbourRays[n].collider.gameObject.GetComponent<Terrain>();
    44.                 }
    45.             }
    46.             //set terrain array
    47.             if(terrainNeighbours[4] != null)
    48.             {
    49.                 terrainNeighbours[4].SetNeighbors(terrainNeighbours[0],terrainNeighbours[1],terrainNeighbours[2],terrainNeighbours[3]); // set neighbours
    50.                 terrainNeighbours[4].basemapDistance = 1500;
    51.                 terrainNeighbours[4].Flush();
    52.                 terrainNeighbours[4].heightmapPixelError = 0.0f;
    53.             }
    54.         }
    I can get it to avoid crashing by adding the line:
    Code (csharp):
    1.                     terrainNeighbours[n].SetNeighbors(null,null,null,null); // set neighbours
    In the collider for loop, but obviously this is counterintuitive as it basically resets the neighboring I'm trying to achieve. Any help would be much appreciated.