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 ?
function Start () {
//Donnees de generation
var X_dim = Random.Range(10, 20);
var Z_dim = Random.Range(10, 20);
var Y_dim_min = Random.Range(-5 , 0);
var Y_dim_max = Random.Range(0, 5);
var terrain = new int[X_dim,Z_dim];
//Generation random
/*
for (var x=1 ; x<=X_dim ; x++)
{
for (var z=1 ; z<=Z_dim ; z++ )
{
var y = Random.Range(Y_dim_min, Y_dim_max);
terrain[x,z] = y;
}
}
*/
//Generation programmee
for (var x = 0; x < X_dim ; x++) {
for (var z = 0; z < Z_dim; z++) {
terrain[x,z] = -6;
}
}
terrain == -6;
var Nb_case = X_dim*Z_dim;
var Temp = 1;
x_pos = Random.Range(0 , X_dim);
z_pos = Random.Range(0 , Z_dim);
var x_new_pos;
var z_new_pos;
var y = Random.Range(Y_dim_min, Y_dim_max);
terrain[x_pos,z_pos] = y;
while(Temp < Nb_case)
{
var direction = Random.Range(1, 5);
var high = Random.Range(-1, 2);
if(direction == 1 && z_pos < Z_dim + 1){z_new_pos = z_pos + 1;} //Haut
if(direction == 2 && x_pos < X_dim + 1){x_new_pos = x_pos + 1;} //Droite
if(direction == 3 && z_pos > 1){z_new_pos = z_pos - 1;} //Bas
if(direction == 4 && x_pos > 1){x_new_pos = x_pos - 1;} //Gauche
if(terrain[x_new_pos,z_new_pos] == - 6){
terrain[x_new_pos,z_new_pos] = terrain[x_pos,z_pos] + high;
x_pos = x_new_pos;
z_pos = z_new_pos;
Temp = Temp + 1;
}else{
x_pos = Random.Range(0 , X_dim);
z_pos = Random.Range(0 , Z_dim);
}
}
//Creation
for (x = 0; x < X_dim ; x++) {
for (z = 0; z < Z_dim; z++) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3 (x, terrain[x,z], z);
}
}
}
Thank you !