The question is answered, right answer was accepted
While loop won't work, it freezes Unity
So I'm trying to use a while loop to have my program find a path through an 8x8 array. However on launch, Unity stops responding and has to be closed. Can someone help explain why my code doesn't work as I don't see anything wrong with it as the condition for it to end is changed. Here is the source code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour {
public int counter = 0;
int[,] LevelData = new int[8,8]{
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
void Start () {
Generate();
}
void Generate(){
//picks a random location for the level to start and the second room, then generates a random path to the bottom of the level
int LevelStartX = Random.Range(0,8);
int LevelStartY = Random.Range(0,5);
int LevelX = 0;
int LevelY = 0;
LevelData[LevelStartX,LevelStartY] = 1;
Debug.Log(LevelStartX);
Debug.Log(LevelStartY);
int Direction = Random.Range(0,3);
if(Direction==0){
if(LevelStartX==0){
LevelData[LevelStartX+1,LevelStartY]=1;
LevelX = LevelStartX+1;
LevelY = LevelStartY;
}else{
LevelData[LevelStartX-1,LevelStartY]=1;
LevelX = LevelStartX-1;
LevelY = LevelStartY;
}
}else if(Direction==1){
if(LevelStartX==7){
LevelData[LevelStartX-1,LevelStartY]=1;
LevelX = LevelStartX-1;
LevelY = LevelStartY;
}else{
LevelData[LevelStartX+1,LevelStartY]=1;
LevelX = LevelStartX+1;
LevelY = LevelStartY;
}
}else{
LevelData[LevelStartX,LevelStartY+1]=1;
LevelX = LevelStartX;
LevelY = LevelStartY+1;
}
while(LevelY!=7){
int NewDirection = Random.Range(0,3);
if(NewDirection==0){
if(LevelX!=0){
if(LevelData[LevelX-1,LevelY]==0){
LevelData[LevelX-1,LevelY]=1;
LevelX=LevelX-1;
}
}
}else if(NewDirection==1){
if(LevelX!=7){
if(LevelData[LevelX+1,LevelY]==0){
LevelData[LevelX+1,LevelY]=1;
LevelX=LevelX+1;
}
}
}
else if(NewDirection==2){
if(LevelY!=7){
if(LevelData[LevelX,LevelY-1]==0){
LevelData[LevelX,LevelY+1]=1;
LevelY=LevelY+1;
}
}
}
}
}
}
I slightly changed how the program works and now it sometimes works but sometimes stops responding. Here is the new source code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : $$anonymous$$onoBehaviour {
public int counter = 0;
int LevelX = 0;
int LevelY = 0;
int[,] LevelData = new int[8,8]{
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
void Start () {
Generate();
}
void RandomPath(){
int NewDirection = Random.Range(0,3);
if(NewDirection==0){
if(LevelX!=0){
if(LevelData[LevelX-1,LevelY]==0){
counter++;
LevelData[LevelX-1,LevelY]=counter;
LevelX=LevelX-1;
}
}
}else if(NewDirection==1){
if(LevelX!=7){
if(LevelData[LevelX+1,LevelY]==0){
counter++;
LevelData[LevelX+1,LevelY]=counter;
LevelX=LevelX+1;
}
}
}
else if(NewDirection==2){
if(LevelY!=7){
if(LevelY!=0){
if(LevelData[LevelX,LevelY-1]==0){
counter++;
LevelData[LevelX,LevelY+1]=counter;
LevelY=LevelY+1;
}
}else{
counter++;
LevelData[LevelX,LevelY+1]=counter;
LevelY=LevelY+1;
}
}
}
}
void Generate(){
//picks a random location for the level to start and the second room, then generates a random path to the bottom of the level
int LevelStartX = Random.Range(0,8);
int LevelStartY = Random.Range(0,5);
counter++;
LevelData[LevelStartX,LevelStartY] = counter;
Debug.Log(LevelStartX);
Debug.Log(LevelStartY);
int Direction = Random.Range(0,3);
if(Direction==0){
if(LevelStartX==0){
counter++;
LevelData[LevelStartX+1,LevelStartY]=counter;
LevelX = LevelStartX+1;
LevelY = LevelStartY;
}else{
counter++;
LevelData[LevelStartX-1,LevelStartY]=counter;
LevelX = LevelStartX-1;
LevelY = LevelStartY;
}
}else if(Direction==1){
if(LevelStartX==7){
counter++;
LevelData[LevelStartX-1,LevelStartY]=counter;
LevelX = LevelStartX-1;
LevelY = LevelStartY;
}else{
counter++;
LevelData[LevelStartX+1,LevelStartY]=counter;
LevelX = LevelStartX+1;
LevelY = LevelStartY;
}
}else{
counter++;
LevelData[LevelStartX,LevelStartY+1]=counter;
LevelX = LevelStartX;
LevelY = LevelStartY+1;
}
while(LevelY!=7 || counter==32){
RandomPath();
}
}
}
I tested how often the program crashes by running it 20 times and it ended up crashing 50% of the time.
Answer by DoYouEvenFish · Aug 13, 2017 at 01:37 AM
After debugging using breakpoints, I've realized that the error was one of the checks done in RandomPath if NewDirection equals two. Essentially, the program would get stuck in a corner. However, I have fixed it by changing the check from if(LevelData[LevelX,LevelY-1]==0)
to if(LevelData[LevelX,LevelY-1]==0 || LevelData[LevelX,LevelY-1]!=counter-1)
Follow this Question
Related Questions
MFPS MouseLook Issue, Namespace or Directive not found after Gaia import! 0 Answers
How to get access to other's script var with increment? 0 Answers
Error `UnityEngine.Component' does not contain a definition 1 Answer
How do you copy an image from the clipboard into an ui image? 0 Answers
UI Text: Words at end of line jumping to next line 0 Answers