- Home /
This Map Generator is freezing unity
MapGenerator script: Here is the script that I am learning from a unity game dev. named Sabatian Lague I follow all his tutorials as closely as possible but as I try to click on the map if freezes and this happens when I press play as well! No error I'm running unity under the quickest and lowest settings for the best performance, which still doesn't help the freezing, is there away i can fix this issue so i can continue the game development?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour {
public Transform tilePrefab;
public Transform obsticlePrefab;
public Vector2 mapSize;
[Range(0,1)]
public float outlinePercent;
[Range(0, 1)]
public float obsticlePercent;
List<Coord> allTileCoords;
Queue<Coord> shuffledTileCoords;
public int seed = 10;
Coord mapCenter;
void Start() {
GenerateMap();
}
public void GenerateMap()
{
allTileCoords = new List<Coord>();
for (int x = 0; x < mapSize.x; x ++)
{
for (int y = 0; y < mapSize.y; y ++)
{
allTileCoords.Add(new Coord(x, y));
}
}
shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), seed));
mapCenter = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);
string holderName = "GeneratedMap";
if (transform.FindChild(holderName))
{
DestroyImmediate(transform.FindChild(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
for (int x = 0; x < mapSize.x; x++)
{
for (int y = 0; y < mapSize.y; y++)
{
Vector3 tileposition = CoordToPosition(x, y);
Transform newTile = Instantiate(tilePrefab, tileposition, Quaternion.Euler(Vector3.right * 90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
newTile.parent = mapHolder;
}
}
bool[,] obsticleMap = new bool[(int)mapSize.x, (int)mapSize.y];
int obsticleCount = (int)(mapSize.x * mapSize.y * obsticlePercent);
int currentObsticleCount = 0;
for (int i = 0; i < obsticleCount; i ++)
{
Coord randomCoord = GetRandomCoord();
obsticleMap[randomCoord.x, randomCoord.y] = true;
currentObsticleCount ++;
if (randomCoord != mapCenter && MapIsFullyAccessible(obsticleMap,currentObsticleCount))
{
Vector3 obsticlePosition = CoordToPosition(randomCoord.x, randomCoord.y);
Transform newObsticle = Instantiate(obsticlePrefab, obsticlePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
newObsticle.parent = mapHolder;
}
else
{
obsticleMap[randomCoord.x, randomCoord.y] = false;
currentObsticleCount --;
}
}
}
bool MapIsFullyAccessible(bool[,] obsticleMap,int currentObsticleCount)
{
bool[,] mapFlags = new bool[obsticleMap.GetLength(0), obsticleMap.GetLength(1)];
Queue<Coord> queue = new Queue<Coord>();
queue.Enqueue(mapCenter);
mapFlags[mapCenter.x, mapCenter.y] = true;
int accessibleTileCount = 1;
while (queue.Count > 0)
{
Coord tile = queue.Dequeue();
for (int x = -1; x <= 1; x++)
{
for (int y = -1; x <= 1; y++)
{
int neighborX = tile.x + x;
int neighborY = tile.y + y;
if (x==0 || y == 0)
{
if (neighborX >= 0 && neighborX < obsticleMap.GetLength(0) && neighborY >= 0 && neighborY < obsticleMap.GetLength(1))
{
if (!mapFlags[neighborX, neighborY] && !obsticleMap[neighborX, neighborY])
{
mapFlags[neighborX, neighborY] = true;
queue.Enqueue(new Coord(neighborX, neighborY));
accessibleTileCount++;
}
}
}
}
}
}
int targetAccessibleTileCount = (int)(mapSize.x * mapSize.y - currentObsticleCount);
return targetAccessibleTileCount == accessibleTileCount;
}
Vector3 CoordToPosition(int x, int y)
{
return new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
}
public Coord GetRandomCoord()
{
Coord randomCoord = shuffledTileCoords.Dequeue();
shuffledTileCoords.Enqueue(randomCoord);
return randomCoord;
}
public struct Coord
{
public int x;
public int y;
public Coord(int _x, int _y)
{
x = _x;
y = _y;
}
public static bool operator == (Coord c1, Coord c2)
{
return c1.x == c2.x && c1.y == c2.y;
}
public static bool operator !=(Coord c1, Coord c2)
{
return !(c1 == c2);
}
}
}
Map Editor:
using System.Collections;
public static class Utility
{
public static T[]ShuffleArray<T>(T[]array, int seed)
{
System.Random prng = new System.Random(seed);
for (int i=0; i < array.Length -1; i++)
{
int randomIndex = prng.Next(i, array.Length);
T tempItem = array[randomIndex];
array[randomIndex] = array[i];
array[i] = tempItem;
}
return array;
}
}
Answer by Jessespike · Jul 11, 2016 at 08:42 PM
A lot of the time, a freeze means there is an infinite loop. You might want to change this:
for (int y = -1; x <= 1; y++)
now if I change this would i put in an if statement in to say if map generator isnot generating then freeze this section of code to stop the infinite loop?
No, it's not that -- The conditional in the middle of the for loop is using the wrong variable. You're checking the state of x, but modifying y.
never $$anonymous$$d i relized when i C&Ped i never changed one of the x's to a y...