Search Unity

Unity crashes when trying to make marching terrain

Discussion in 'Documentation' started by gigageo, Jan 20, 2019.

  1. gigageo

    gigageo

    Joined:
    Jan 20, 2019
    Posts:
    1
    Hello i am trying to make terrain that marches when player moves but for now i am trying to just build 3 x 3 terrain around player

    this is code i wrote:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Marching : MonoBehaviour
    6. {
    7.     public Transform Player;
    8.  
    9.     Transform[] ChunkCopy;
    10.     Transform Chunk;
    11.  
    12.     Vector3[] ChunkPositions;
    13.  
    14.     int PositionOffset = 100;
    15.  
    16.     int ChunkX;
    17.     int ChunkY;
    18.  
    19.     void Start()
    20.     {
    21.         Generate();
    22.     }
    23.  
    24.     void Generate ()
    25.     {
    26.         ChunkX = 3;
    27.         ChunkY = 3;
    28.  
    29.         Chunk = this.gameObject.transform;
    30.  
    31.         ChunkPositions = new Vector3[ChunkX * ChunkY];
    32.         ChunkCopy = new Transform[ChunkX * ChunkY];
    33.  
    34.         for (int ChunkIndexZ = 0, ChunkIndexX = 0, index = 0, x = 0;x< ChunkX;x++)
    35.         {
    36.             for (int y = 0;y< ChunkY; y++)
    37.             {
    38.                 ChunkPositions[index] = new Vector3(Player.position.x - PositionOffset, 0, Player.position.z - PositionOffset);
    39.  
    40.                 index++;
    41.                 ChunkIndexX += 10;
    42.             }
    43.             ChunkIndexZ += 10;
    44.         }
    45.  
    46.         for (int Index = 0, x = 0; x <= ChunkX; x++)
    47.         {
    48.             for (int y = 0; y <= ChunkY; y++)
    49.             {
    50.                 ChunkCopy[Index] = Instantiate(Chunk, ChunkPositions[Index], Quaternion.identity);
    51.             }
    52.         }
    53.     }  
    54. }
    55.  
    i am not sure what is wrong with it. when i try to play unity crashes