- Home /
This code crashes unity, please help!
I use this code to generate a world in space of 500x500 and to then load it, it's not finished but for some reason it crashes when i start the GenerateWorld(); function.
var generate : boolean = false;
var load : boolean = false;
var objects : GameObject[];
var world = new Array(500);
/*
1 - sun
2 - home planet (UNIQUE)
3 - BLACK HOLE
4 -> 11 - normal planet
12 -> 15 - hot planet
16 - cold planet
*/
function SpaceCheck(x : int, y : int)
{
for(var i = x-3; i <= x+3; i++)
{
for(var j = y-3; j <= y+3; j++)
{
if(world[i][j]!=0)
{
return 0;
}
}
}
return 1;
}
function GenerateWorld()
{
Debug.Log("Starting to generate");
world[250][250] = 1;
world[243][250] = 2;
Debug.Log("Created home planet and solar system");
for(var i = 20; i <= 480; i++)
{
for(var j = 20; j < 480; j++)
{
Debug.Log(i+" "+j);
var a : float = Random.Range(0.0, 10.0);
if(a >= 9.0)
{
Debug.Log("Generated a Star");
world[i][j] = 1;
for(var k = i-10; k <= i+10; k++)
{
for(var l = j-10; l <= j+10; l++)
{
var b : float = Random.Range(0.0,10.0);
if( (i-k)*(i-k)+(l-j)*(l-j)>=18 && b>=7.0)
{
if(SpaceCheck(k,l))
{
var planet : int = 0;
var dist : int = (i-k)*(i-k)+(l-j)*(l-j);
if(dist >=18 && dist <=40)
{
planet = Random.Range(12,15);
}
if(dist>40 && dist <=150)
{
planet = Random.Range(4,11);
}
if(dist>150)
{
planet = 16;
}
world[k][l] = planet;
}
}
}
}
}
}
}
Debug.Log("done generatin");
}
function GetWorldData()
{
Debug.Log("done getting info");
}
function LoadWorld()
{
for(var i = 1; i <= 500; i++)
{
for(var j = 1; j < 500; j++)
{
if(world[i][j])
{
Instantiate (objects[world[i][j]], Vector3(i, j, 0), Quaternion.identity);
}
}
}
Debug.Log("done loading");
}
function Update ()
{
if(generate)
{
generate = false;
GenerateWorld();
}
if(load)
{
load = false;
GetWorldData();
LoadWorld();
}
}
function Start()
{
for (var i = 0; i < 500; i++)
{
world[i] = new Array(500);
}
}
It's not clear at all what that code is supposed to do. Where do all these numbers come from? What do they mean? Why 3? Why 0? Why 1? Why 250? Why 20? Why 400? Why 10.0? Why 9? Why 18? Why 40? Why 15? Why 12? Why 16? Why 4? Why 11? Why -10? Why 150?
Even you can't tell me you can remember what all of those are supposed to mean???
The values of world at i,j coordinates represent an object(the way it's decided is in the comment of the code at the top). In the function SpaceCheck we check the neighbouring fields around a certain coordinate to see if there is another object there, for example in this case: 000 0X0 000 the function would return 1 meaning it is ok for the planet/start/whatever to be placed there The random variables are there just to randomly generate if there is going to be a planet or not at a certain coordinate. I could go on about this for a while but i don't think it's important for the reason it's crashing.
ok so here is what the code(at least the GenerateWorld() part) does in short:
check if the order to generate has been given
start at coordinates 20, 20 and randomly generate a number for each coordinate
if the number is >=9.0 generate a start (place 1 at world(i,j))
create a solar system for the star, starting from starX-10, starY-10 coordinates to starX+10, starY+10 coordinates and generate a random number
if the number is >= 7.0 check if there are already any planets nearby in the "world" matrix(2d array) by using the function SpaceCheck(x : int, y : int)
if the fucntion returns 1, the area is clear and planet can be generated
based on the distance from the star(center of the solar system) generate randomly one of 3 types of planets(the values for them are written in the comments)
I think it would help increadably if you at least commented your code, or even just used constants that describe what each of the values mean. It's very hard to debug without knowing what it's doing, impossible when there's not even a way to find out.
Also, you should be using a different collection for your world. As it seems you're just creating a 500x500 grid, you should probably be using build-in arrays (`int[,]`)
Your answer
Follow this Question
Related Questions
Script Crashing Unity 1 Answer
Making opening door requires a Key 2 Answers
What is wrong with this script? 2 Answers
need help, with greater than statment 1 Answer
How to get a value from an array within another script. 1 Answer