尝试使用Stack.Pop()时出现InvalidOperationException

时间:2014-02-17 01:06:50

标签: unity3d stack depth-first-search

我得到一个完全没有任何意义的例外。我正在尝试使用这种深度优先算法来制作随机迷宫:

http://www.mazeworks.com/mazegen/mazetut/

我已经完成了大部分工作,但是当我运行程序时,我收到了这个错误:

  

InvalidOperationException:由于当前操作,操作无效   对象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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据the documentation,当堆栈为空时会发生此异常。

如果您在调试时遇到问题,最好只在 if / else 块中添加UnityEngine.Debug.Log(m_currentCell.name);来跟踪堆栈的状态,或者只是每次打印整个堆栈在输入 if 语句之前。