- Home /
Somthing about my script i making unity hang if i set the room count any higher that 0 can't figure out what though ?
Hi something about the script i have written to randomly generate a dungeon is making unit hang, and i can't think what is and cause of the hang can't debug it very well.
Here is the code: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Genarator : MonoBehaviour {
public List<GameObject> right = new List<GameObject>();
public List<GameObject> left = new List<GameObject>();
public List<GameObject> up = new List<GameObject>();
public List<GameObject> down = new List<GameObject>();
public int roomSize = 10;
public int roomCount = 50;
private int roomsMade = 0;
private int[][] map;
// Use this for initialization
void Start () {
startGeneration();
}
// Update is called once per frame
void Update () {
}
private void startGeneration()
{
GameObject first = right[0];
Instantiate(first, Vector3.zero,Quaternion.identity);
GetRoomsToGen(first);
}
private void GetRoomsToGen(GameObject room)
{
DirectionsAvailable temp = room.GetComponent<DirectionsAvailable>();
if(temp.Right && roomsMade < roomCount)
{
GenarateRoomRight(room);
roomsMade++;
}
if(temp.Left && roomsMade < roomCount)
{
GenarateRoomLeft(room);
roomsMade++;
}
if(temp.Up && roomsMade < roomCount)
{
GenarateRoomUp(room);
roomsMade++;
}
if(temp.Down && roomsMade < roomCount)
{
GenarateRoomDown(room);
roomsMade++;
}
}
private void GenarateRoomRight(GameObject previousRoom)
{
int rand = Random.Range(0,right.Count-1);
GameObject tempRoom = right[rand];
Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
GetRoomsToGen(tempRoom);
}
private void GenarateRoomLeft(GameObject previousRoom)
{
int rand = Random.Range(0,left.Count-1);
GameObject tempRoom = left[rand];
Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
GetRoomsToGen(tempRoom);
}
private void GenarateRoomUp(GameObject previousRoom)
{
int rand = Random.Range(0,up.Count-1);
GameObject tempRoom = up[rand];
Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
GetRoomsToGen(tempRoom);
}
private void GenarateRoomDown(GameObject previousRoom)
{
int rand = Random.Range(0,down.Count-1);
GameObject tempRoom = down[rand];
Instantiate(tempRoom, previousRoom.transform.position,Quaternion.identity);
GetRoomsToGen(tempRoom);
}
}
Any help would be greatly appreciated :) Thanks
Update: I have narrowed it down to the GenerateRoom methods and i was wondering it it was been caused by them all trying to get the rooms at once?
Thanks again
Answer by robertbu · Apr 30, 2014 at 09:25 PM
You have a recursive function, but you are not increasing 'roomsMade' in the right place. For example, start with the base case of roomCount is 1 and roomsMade = 0;
On Line 31 you call GetRoomsToGen()
On Line 42 you call GenarateRoomRight();
On Line 68 you again call GetRoomsToGen();
On Line 42 you again call GenerateRoomRight();
Unless there is something in temp.Right that would stop the cycle, you've created an infinite recursion. The program will hang, and then the stack will blow up. As a starting point for a fix, move the 'roomsMade++;' to just below the four Instantiate() calls so that the value is incremented before you next call GetRoomsToGen().
Thank you :) i don't know why i didn't notice it i think i just glazed over :S thank you again :D