Hey, I’m starting to play with Unity, I want to rewrite a simple TD game I made in Java. Unfortunately very fast I bumped on a problem. When I click play my Unity freezes, I have to kill a task. I have no idea what’s wrong, I tried some Debug.Logs but I can’t even view them, can’t access unity after the play button. I searched my code for infinite loops and I can’t find any (being able to see console logs would help).
So far the code only has 3 classes, here’s the first simple one to hold the static variables:
public class GlobalVariables : MonoBehaviour {
//The width of the board in blocks
public static int boardWidth = 40;
//The height of the board in blocks
public static int boardHeight = 36;
//The lenghth of the road in blocks;
public static int pathLength = 30;
//Pixel size of a block
public static int blockSize = 64;
void Start () {
}
void Update () {
}
}
Second class prepares a matrix, which I use for drawing:
public class MapCreator
{
//Array containing the path
int[] directionLayout = new int[GlobalVariables.pathLength];
public int[] getDirectionLayout ()
{
return directionLayout;
}
public MapCreator() {}
/**
*Returns an index layout of game board.
*/
public int[,] prepareMap ()
{
//X and Y position of start and end
int startValueX;
int startValueY;
//Used while creating a path
int tempValueX;
int tempValueY;
int xDirection;
int yDirection;
int pathRemaining = GlobalVariables.pathLength;
//Used for random
int[] randomDirection = {-1, 1};
//Return matrix
int[,] mapIndexLayout = new int[GlobalVariables.boardWidth, GlobalVariables.boardHeight];
for (int i = 0; i < GlobalVariables.boardWidth; i++) {
for (int k = 0; k < GlobalVariables.boardHeight; k++) {
mapIndexLayout [i, k] = 0;
Debug.Log("Filling with 0, step: " + i + ", " + k);
}
}
startValueX = Random.Range (1, GlobalVariables.boardWidth - 2);
startValueY = Random.Range (1, GlobalVariables.boardHeight - 2);
tempValueX = startValueX;
tempValueY = startValueY;
mapIndexLayout [startValueX, startValueY] = 2;
while (pathRemaining > 0) {
xDirection = randomDirection [Random.Range (0, 2)];
if (xDirection == 1) {
yDirection = -1;
} else {
yDirection = 1;
}
if (1 < (tempValueX + xDirection) && (tempValueX + xDirection) < (GlobalVariables.boardWidth - 2)
&& 1 < (tempValueY + yDirection) && (tempValueY + yDirection) < (GlobalVariables.boardHeight - 2)) {
if (mapIndexLayout [tempValueX + xDirection, tempValueY + yDirection] == 0) {
if (pathRemaining > 1) {
mapIndexLayout [tempValueX + xDirection, tempValueY + yDirection] = 1;
} else {
mapIndexLayout [tempValueX + xDirection, tempValueY + yDirection] = 3;
}
if (xDirection == 0 && yDirection == 1) {
directionLayout [40 - pathRemaining] = 8;
} else if (xDirection == 0 && yDirection == -1) {
directionLayout [40 - pathRemaining] = 2;
} else if (xDirection == 1 && yDirection == 0) {
directionLayout [40 - pathRemaining] = 6;
} else if (xDirection == -1 && yDirection == 0) {
directionLayout [40 - pathRemaining] = 4;
}
pathRemaining -= 1;
tempValueX += xDirection;
tempValueY += yDirection;
}
}
Debug.Log("Creating path, remains: " + pathRemaining + " blocks.");
}
Debug.Log("Path Created");
return mapIndexLayout;
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
The last class takes the matrix and draws tiles based on it (well, it does not, but that’s my goal :P)
public class MapDrawer : MonoBehaviour
{
//Object containing prepareMap method
MapCreator mapCreator = new MapCreator ();
//Textures has to be chosen in inspector
public Texture grassTexture;
public Texture roadTexture;
public Texture startTexture;
public Texture endTexture;
//Temporary texture
/**
* Method drawing a board
*/
void drawMap ()
{
Debug.Log("Begin drawing the map.");
//Index layout of game board
int[,] mapIndexLayout = mapCreator.prepareMap ();
//Temporary texture
Texture tempTexture;
if (!grassTexture && !roadTexture && !startTexture && !endTexture) {
Debug.LogError ("Assign Textures in the inspector.");
return;
}
for (int i = 0; i < GlobalVariables.boardWidth; i++) {
for (int k = 0; k < GlobalVariables.boardHeight; k++) {
tempTexture = setTextureById(mapIndexLayout[i, k]);
Debug.Log("Drawing at position: " + i + ", " + k);
GUI.DrawTexture (new Rect ((i * GlobalVariables.blockSize), (k * GlobalVariables.blockSize), GlobalVariables.blockSize, GlobalVariables.blockSize), tempTexture, ScaleMode.ScaleToFit, true, 0);
}
}
}
Texture setTextureById(int id) {
Debug.Log("Setting texture");
if (id == 0) {
return grassTexture;
} else if (id == 1) {
return roadTexture;
} else if (id == 2) {
return startTexture;
} else if (id == 3) {
return endTexture;
}
return null;
}
// Use this for initialization
void Start ()
{
drawMap ();
}
// Update is called once per frame
void Update ()
{
}
}
I would be grateful for any help. By the way, leaving the unity untouched doesn’t help, but I noticed that memory usage keeps rising, slowly but steady, from ~~150000k to ~~700000k when I killed the task.