Call a method of a class derived from MonoBehaviour from a class that isn't.

I’m creating a version of Minesweeper. I have a class that derives from MonoBehaviour called GameManager and a class that doesn’t derive from MonBehaviour called Grid. When GameManager creates an instance of Grid, it passes itself in through Grid’s constructor so that Grid will be able to call a public method in GameManager. However, whenever Grid calls a method of GameManager, Unity crashes.

Here’s Grid’s constructor:

public Grid ( GameManager gm, int rowNum, int colNum, int depth, int bombCount )
	{
		m_dummy = new Cell( this, Cell.CellState.clear, false );
		m_rowNum = rowNum;
		m_colNum = colNum;
		m_depth = depth;
		m_bombCount = bombCount;
		m_cellsToClear = ( rowNum * colNum ) - bombCount;
		m_gm = gm;

		create ();
		init();
	}

This is where GameManager creates an instance of Grid:

m_grid = new Grid( this, 10, 10, 10, 25 );

Here’s where Grid calls the method from GameManager:

int cRow = cell.getRow();
int cCol = cell.getCol();
int cDepth = cell.getDepth();

m_gm.cubeCleared( cRow, cCol, cDepth );

Here’s the method from GameManager that is called:

public void cubeCleared ( int row, int col, int depth )
	{
		Destroy( getCube( row, col, depth ).gameObject );
		print( "cube cleared at "  + row + ", " + col + ", " + depth );
	}

Everything in all my scripts works well and there are no compile errors. I tried creating a very simple method in GameManager that simply prints something and calling it from Grid and Unity still crashes.

Is there something about classes derived from MonoBehaviour that doesn’t let classes that aren’t derived from it call their methods?

[EDIT]
Upon further analysis I found the following in addition to the problem described:

The game includes an auto-clear function that will clear away any cells surrounding a cell that is clicked on and has no mines next to it. I’m implementing that with the following function:

 public void cellClear ( Cell cell )
     {
         int cRow = cell.getRow();
         int cCol = cell.getCol();
         int cDepth = cell.getDepth();
 
         m_gm.cubeCleared( cRow, cCol, cDepth );
 
         if ( --m_cellsToClear == 0 )
         {
             // you win
         }
         else if ( cell.getAdjBombNum() == 0 )
         {
             for ( int dr = -1; dr <= 1; dr++ )
             {
                 for ( int dc = -1; dc <= 1; dc++ )
                 {
                     for ( int dd = -1; dd <= 1; dd++ )
                     {
                         int r = cell.getRow() + dr;
                         int c = cell.getCol() + dc;
                         int d = cell.getDepth() + dd;
                         
                         Cell tCell = getCell( r, c, d );
                         tCell.lClick();
                     }
                 }
             }
         }
     }

The method cell.lClick() changes some states in the cell and calls cellClear() if the cell didn’t have any mines and wasn’t flagged. cellClear() is recursively called until there are either no more cells left to clear, or the cleared cell doesn’t have any empty cells next to it. The method works when I delete the recursiveness (meaning that the auto clear functionality if taken out) or when I delete the call to GameManager (m_gm.cubeCleared).

The problem isn’t an infinite loop like I had suspected when I first came across the problem as it runs fine when I delete the reference to the GameManager. It also no longer appears to be the call to GameManager itself anymore. It’s only when it tries to call GameManager as it loops through itself auto-clearing cubes.

I am not really sure why the editor is crashing. You can try looking into the unity log and see what it is saying. Either way this is not the best way to do this in my opinion. In my opinion it would be better for you to add delegates to your Grid class that the GameManager would then subscribe to and act upon them.