전혀 이해가되지 않는 예외가 발생합니다.Stack.Pop()을 사용할 때 InvalidOperationException이 발생합니다.
http://www.mazeworks.com/mazegen/mazetut/
내가 모든 것을 구현 대부분 가지고 있지만, 나는이 프로그램을 실행할 때, 나는이 오류를 얻을 :
을 나는 임의의 미로를 만들기 위해이 깊이 우선 알고리즘을 사용하려고 해요InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.Stack 1[UnityEngine.GameObject].Pop()
불행한 점은 스택이 인스펙터에 나타나지 않아서 스택의 '현재 상태'를 볼 수 없다는 것입니다.
이 오류는 디버거를 사용할 때도 발생하지 않습니다. 스택이 비어있을 때
/***************************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MazeGenerator : MonoBehaviour
{
public GameObject[] m_totalCells; // all the cells in the level
public GameObject[] m_walls; // all the walls in the level
public List<GameObject> m_visited; // list of each visited cell
public Stack<GameObject> m_cellStack; // last-in, first-out stack for generating the maze
public GameObject m_currentCell; // the cell currently being looked at
public int m_visitedCells = 0; // how many cells have been looked at
public bool m_puzzleCreated; // has the puzzle been made yet
/*****************************************************************************/
/*
Description:
Use this for initialization.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void Start()
{
m_totalCells = GameObject.FindGameObjectsWithTag("Cell");
m_walls = GameObject.FindGameObjectsWithTag("Wall");
//initialize the stack
m_visited = new List<GameObject>();
}
/*****************************************************************************/
/*
Description:
Make a random maze using the depth-field algorithm.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void GeneratePuzzle()
{
// make sure all the cells have found their neighbors and walls
foreach(GameObject cell in m_totalCells)
{
cell.GetComponent<Cell>().FindAdjacents();
cell.GetComponent<Cell>().FindWalls();
cell.GetComponent<Cell>().FindNeighbors();
}
m_cellStack = new Stack<GameObject>();
// grab a randon cell
int randomizer = Random.Range(0, m_totalCells.Length);
m_currentCell = m_totalCells[randomizer];
m_visitedCells = 1;
while(m_visitedCells < m_totalCells.Length)
{
m_visited.Add(m_currentCell);
if(m_totalCells[randomizer].GetComponent<Cell>().FindNeighbors() == true)
{
m_cellStack.Push(m_currentCell);
m_currentCell = m_currentCell.GetComponent<Cell>().BreakWall();
m_visitedCells++;
}
else
{
/*********************************/
/* This is where I Pop() */
/*********************************/
m_currentCell = m_cellStack.Pop();
}
}
foreach(GameObject wall in m_walls)
{
if(wall.gameObject.GetComponent<Wall>().m_destroyed == true)
{
Destroy(wall);
}
}
}
/*****************************************************************************/
/*
Description:
Update is called once a frame.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void Update()
{
if(Input.GetMouseButtonDown(0) && m_puzzleCreated == false)
{
m_puzzleCreated = true;
GeneratePuzzle();
}
}
}