Unity is not loading play mode while i'm using this script, i would like to know why?,Unity is not loading play mode while I use this script, i would like to know why
I'm trying to make a random map generator with blocks of different heigth.I want to generate a hole in the center of the map, but when I lunch play mode, i find myself wating forever.
This is my code:
public float mapX;
public float mapY;
public float insideX;
public float insideY;
float constantX;
float constantY;
public GameObject prefab;
Vector3 myPosition;
// Start is called before the first frame update
void Start()
{
constantX = mapX;
constantY = mapY;
}
// Update is called once per frame
void Update()
{
while (mapY > -constantY)
{
while (mapY > insideY||mapY<-insideY)
{
while (mapX > -constantX)
{
myPosition = new Vector3(mapX - 0.5f, 0, mapY - 0.5f);
GameObject Myprefab = Instantiate(prefab, myPosition, Quaternion.identity) as GameObject;
Myprefab.transform.parent = GameObject.Find("outsideMap").transform;
mapX--;
}
mapX = constantX;
mapY--;
}
while (mapY < insideY ||mapY>-insideY)
{
if (mapX < insideX || mapX > -insideX) {
mapX++;
}
while (mapX>insideX||mapX<-insideX) {
myPosition = new Vector3(mapX - 0.5f, 0, mapY - 0.5f);
GameObject Myprefab = Instantiate(prefab, myPosition, Quaternion.identity) as GameObject;
Myprefab.transform.parent = GameObject.Find("outsideMap").transform;
mapX--;
}
mapX = constantX;
mapY--;
}
}
}
},
Also, what values are you using for mapX, mapY, insideX, insideY.
I want to double check your while statements, to make sure you are not ending up in an endless loop here.
Answer by Larry-Dietz · Dec 06, 2019 at 08:19 PM
First thing I notice is that you are generating this map over and order, every frame. Eventually I would expect that you will get an out of memory error.
First thing I would try, is to rename that Update method to GenerameMap, then call it from Start, so the map is generated only 1 time.
$$anonymous$$y guess at this point is an endless loop.
What values are you using for mapX, mapY, insideX and insideY?
Your answer
Follow this Question
Related Questions
I can't load play mode 2 Answers
,UNEXPECTED SYMBOL VAR 1 Answer
How to make a player instantly reach to a change in gravity? 2 Answers
Moving an Object to a random position 3 Answers
Unity C# how to Foreach variable in Model/Contract 0 Answers