Hi!
I’m currently working on a school project. I’m trying to make a random map generator that spawns bricks on the map random every time you start the level, nothing special just spawns random blocks or empty places. this codes is the only code that is in the game and as fast as i start Unity it freezes so i thought i was an infinite loop. I have only 2 while loops in the code witch i believe to be the problem but no matter what i do i cant break the loop or debug it. So I’m wondering if someone can help me with what I’m doing wrong, there must be some problem I’m not seeing.
Here is the code int the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateMap : MonoBehaviour {
public int xRows;
public int yRows;
public int emptySpacesTotal;
public float xPosStart;
public float yPosStart;
public GameObject[] Bricks;
public int[] brickToCreate;
public int xSpacingMultiplyer = 2;
public int ySpacingMultiplyer = 1;
private int[] brickCheck;
private int emptySpacesCount = 0;
private int blockCount = 0;
private Transform Map;
private List<Vector3> gridPos = new List<Vector3>();
void Awake () {
/*Make sure that you cant set different sizes on game object array and int array*/
if (Bricks.Length != brickToCreate.Length) {
Debug.LogError (string.Format ("Size of {0} array needs to be equal to size of {1} array! Called from (game object and (script)) {2} ", Bricks.ToString (), brickToCreate.ToString (), GetComponent<MonoBehaviour> ().ToString ()));
Debug.Break();
} else {
/*create editble private copy of array*/
brickCheck = brickToCreate;
}
/*make sure that x and y dont over/under-extend the gameboard*/
xRows = Mathf.Clamp (xRows, 1, 17);
yRows = Mathf.Clamp (yRows, 1, 18);
/*make sure that it cant be more empty spaces then half of the rows created*/
if (emptySpacesTotal > Mathf.RoundToInt((yRows * xRows) / 2)) {
emptySpacesTotal = Mathf.RoundToInt((yRows * xRows) / 2);
}
/*get parent*/
Map = GameObject.FindGameObjectWithTag("GameController").transform;
}
private void InizializeGridPos(){
for (int x = 0; x < xRows; x++) {
for (int y = 0; y < yRows; y++) {
gridPos.Add (new Vector3 ( Map.position.x + (xPosStart + x * xSpacingMultiplyer),
Map.position.y - (yPosStart + y * ySpacingMultiplyer), 0.0f));
}
}
}
private int RandomNumber(int min, int max){
int randomNumber = Random.Range (min, min);
return randomNumber;
}
private void SubtractCreatedBrick(int pos){
if (brickCheck[pos] > 0) {
brickCheck[pos] = brickCheck[pos] - 1;
blockCount++;
}
}
private bool IsPosZero(int pos){
if (brickCheck[pos] == 0) {
return true;
} else {
return false;
}
}
private bool ArrayIsNotEmpty(){
int BrickTypeIsZero = 0;
for (int i = 0; i < brickCheck.Length; i++) {
if (brickCheck *== 0) {*
-
BrickTypeIsZero++;*
-
}*
-
}*
-
if (BrickTypeIsZero < brickCheck.Length - 1) {*
-
return true;*
-
} else {*
-
return false;*
-
}*
-
}*
-
private Vector3 GetRandomPos(){*
-
int randomPos = RandomNumber (0, gridPos.Count);*
-
Vector3 pos = gridPos[randomPos];*
-
gridPos.RemoveAt(randomPos);*
-
return pos;*
-
}*
-
private GameObject GetBrickToSpawn(){*
-
int randomBlock = RandomNumber (0, Bricks.Length);*
-
if (ArrayIsNotEmpty ()) {*
-
while (brickCheck[randomBlock] == 0) {*
-
Debug.Log (string.Format ("brickChekc = {0}", brickCheck[randomBlock]));*
-
Debug.Break();*
-
randomBlock = RandomNumber (0, Bricks.Length);*
-
}*
-
SubtractCreatedBrick(randomBlock);*
-
return Bricks [randomBlock];*
-
} else {*
-
return null;*
-
}*
-
}*
-
private int Roll_50_50(){*
-
int randomNumber = RandomNumber (0, 100);*
-
if (randomNumber < 50) {*
-
return 1;*
-
} else {*
-
return -1;*
-
}*
-
}*
-
private void SpawnBricks(){*
-
InizializeGridPos();*
-
while(gridPos.Count > 0){*
-
Debug.Log (string.Format ("gridCount = {0}", gridPos.Count));*
-
Debug.Break ();*
-
GameObject BrickToSpawn = GetBrickToSpawn();*
-
if ( ((Roll_50_50 () == 1) && (emptySpacesCount < emptySpacesTotal)) || (BrickToSpawn == null) ) {*
-
GetRandomPos();*
-
emptySpacesCount++;*
-
} else {*
-
GameObject _instance = Instantiate (BrickToSpawn, GetRandomPos(), Quaternion.identity) as GameObject;*
-
_instance.transform.SetParent(Map);*
-
}*
-
}*
-
}*
-
void Start(){*
-
SpawnBricks();*
-
}*
}