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

Problem of generation

Discussion in 'Scripting' started by Olivier felix, Mar 28, 2015.

  1. Olivier felix

    Olivier felix

    Joined:
    Mar 27, 2015
    Posts:
    5
    Hi,
    I was trying to create a random map using blocs (a bit like minecraft) for a fps but the error "NullReferenceException" keep showing up.
    I looked in other threads and it was written to create a gameobject but mine is a matrix (and I think the position within the matrix). So I don't really know how to do that.
    Any help ?
    Code (JavaScript):
    1. function Start () {
    2.  
    3. //Donnees de generation
    4.     var X_dim = Random.Range(10, 20);
    5.     var Z_dim = Random.Range(10, 20);
    6.     var Y_dim_min = Random.Range(-5 , 0);
    7.     var Y_dim_max = Random.Range(0, 5);
    8.     var terrain = new int[X_dim,Z_dim];
    9.  
    10. //Generation random
    11. /*
    12.     for (var x=1 ; x<=X_dim ; x++)
    13.     {
    14.         for (var z=1 ; z<=Z_dim ; z++ )
    15.         {
    16.             var y = Random.Range(Y_dim_min, Y_dim_max);
    17.             terrain[x,z] = y;
    18.         }
    19.     }
    20. */
    21.  
    22. //Generation programmee
    23.     for (var x = 0; x < X_dim ; x++) {
    24.         for (var z = 0; z < Z_dim; z++) {
    25.             terrain[x,z] = -6;
    26.         }
    27.     }
    28.     terrain == -6;
    29.     var Nb_case = X_dim*Z_dim;
    30.     var Temp = 1;
    31.     x_pos = Random.Range(0 , X_dim);
    32.     z_pos = Random.Range(0 , Z_dim);
    33.     var x_new_pos;
    34.     var z_new_pos;
    35.     var y = Random.Range(Y_dim_min, Y_dim_max);
    36.     terrain[x_pos,z_pos] = y;
    37.     while(Temp < Nb_case)
    38.     {
    39.         var direction = Random.Range(1, 5);
    40.         var high = Random.Range(-1, 2);
    41.      
    42.         if(direction == 1 && z_pos < Z_dim + 1){z_new_pos = z_pos + 1;}    //Haut
    43.         if(direction == 2 && x_pos < X_dim + 1){x_new_pos = x_pos + 1;}    //Droite
    44.         if(direction == 3 && z_pos > 1){z_new_pos = z_pos - 1;}            //Bas
    45.         if(direction == 4 && x_pos > 1){x_new_pos = x_pos - 1;}            //Gauche
    46.      
    47.         if(terrain[x_new_pos,z_new_pos] == - 6){
    48.             terrain[x_new_pos,z_new_pos] = terrain[x_pos,z_pos] + high;
    49.             x_pos = x_new_pos;
    50.             z_pos = z_new_pos;
    51.             Temp = Temp + 1;
    52.         }else{
    53.             x_pos = Random.Range(0 , X_dim);
    54.             z_pos = Random.Range(0 , Z_dim);
    55.         }
    56.      
    57.     }
    58.  
    59. //Creation
    60.  
    61.     for (x = 0; x < X_dim ; x++) {
    62.         for (z = 0; z < Z_dim; z++) {
    63.             var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    64.             cube.transform.position = Vector3 (x, terrain[x,z], z);
    65.         }
    66.     }
    67. }
    Thank you !
     
  2. Olivier felix

    Olivier felix

    Joined:
    Mar 27, 2015
    Posts:
    5
    The error appear on line 47