Hey all,
I have a problem with some random spawning of game objects. I wish to spawn an exit in a randomly created dungeon, but I think my code has an infinite loop somewhere, as Unity freezes when the code is ran. At the moment, the “exit” is just an empty Game Object with a point light attached (so I can easily see it).
var ExitPiece : GameObject //The GO used for the exit. Its just a pointlight at the moment
var DungeonSizeX : int = 3; //The length the dungeon will be, measured in spawned pieces
var DungeonSizeY : int = 3; //The width the dungeon will be, measured in spawned pieces
var PartSize : int = 4 //The dimensions (length and width) of the pieces that are used, measured in Unity units
function GenerateExit () {
Debug.Log("Creating exit");
var SpawnedExit : boolean = false;
while(!SpawnedExit){
P1 = Random.Range(0, DungeonSizeX*PartSize);
P2 = Random.Range(0, DungeonSizeY*PartSize);
ExitPoint = Vector3(P1, 1.25, P2);
hit = RaycastHit;
//Casts a ray at the ExitPoint. If the ray hits something, the exit isnt created
if(!Physics.OverlapSphere(ExitPoint, 1f)){
Instantiate(ExitPiece, ExitPoint, Quaternion.identity);
SpawnedExit = true;
Debug.Log("Exit created at point: " +(ExitPoint.ToString()));
}
else{
Debug.LogError("Exit creation failed");
}
}
}
The code to create the dungeons is here, and this function is called after after GenerateWall.
The pieces I’m using are all 2.5units tall, and 4 units wide, so the overlapSphere should be able to find a place to spawn the exit. I’ve tried reducing the radius, but Unity still freezes.
Any help?