StackOverflowException: The requested operation caused a stack overflow.

I’m trying to make recursive function with hoops but I don’t know how else write:

Way[way].BuildHallway(way, hoop);

using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class Node : MonoBehaviour {
	public static List<System.Action> BuildedHallways = new List<System.Action>();

	public static float falseWay = 0.3f;
	public static float endFalse = 0.5f;
	public static int totalTrueWays = 10;
	public static byte stop = 5;




	public int counTrue = 0;
	public int countFalse = 0;
	public bool builded = false;
	public bool hallBuild = false;
	public bool correctWay = true;
	private byte element;
	public Node[] Way = new Node[6];//F/B/L/R/U/D
	public bool[] hallway = new bool[6]{false, false, false, false, false, false};
	//I think I don't need way
	public void BackWay(Node Back, byte way){
		if (way % 2 == 0) {
			way++;
		}
		else {
			way--;
		}
		Way [way] = Back;
		hallway [way] = true;
	}
	public void GetNewWay(Node Back, int way, bool trueWay, int count, int hoop){
		if (hoop > 0 && !builded) {
			hoop--;
			element = (byte)(Random.Range(1, 6));
			element = 1;
			//Debug.Log (trueWay);
			if (trueWay) {
				//!!!!!!!!!!!!!!!!here we make new hallway and make an ending
				if (totalTrueWays == count) {
					Debug.Log ("finished");
					return;
				}

				counTrue = ++count;
				correctWay = true;


				//witch way

				int i = Random.Range (0, 6);
				i = 0;
				//if way is empty
				RaycastHit hit;

				byte b=0;
				bool made = false;
				if(Physics.Raycast(transform.position, V3HallWay(new Vector3(0,0,0), (byte)i), out hit, 10)){//maybe we don't need hit
					for(; b<6; b++){
						if(! Physics.Raycast(transform.position, V3HallWay(new Vector3(0,0,0), (byte)i), 10)){
							i=b;
							made = true;
							break;
						}
					}
				}
				if(! made){
					//let's make end as there are non able to get through
				}
				//Debug.Log ("here1");
				hallway *= true;*
  •  		//here we create the Node*
    
  •  		Transform temp = MasterPool.CreateMe (0);*
    
  •  		//where it gowes that way it's made*
    
  •  		Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);*
    
  •  		V = V3HallWay (V, (byte)i);*
    
  •  		temp.transform.position = V;*
    
  •  		Node N = temp.GetComponent<Node> ();*
    

_ Way = N;_
* N.BackWay (N, (byte)i);*
* N.GetNewWay (this, i, true, count, hoop);*

* //5 true ways to get to check point*
* if((counTrue % 5) == 0){*
* //Transform checkpoint = MasterPool.CreateMe(12);*
* //checkpoint.position = transform.position;*
* }*
* }*
* else {*
* //!!!here we make new hallways and make the door well we need to merge it with false ways*
* countFalse = ++count;*
* if (stop == countFalse) {*
* Debug.Log (“stop”);*
* return;*
* }*
* }*
* builded = true;*
* // lets create 5 possible false hallways*
_ /for (int i = 0; i < 6; i++) {_
_
//if way is empty*_
* if(! Physics.Raycast(transform.posittion, V3HallWay(new Vector3(0,0,0)(byte)i), out hit, 30)){//maybe we don’t need hit // 30m so that true way has ability to pass arount them*
* if (way != i) {*
* float r = Random.Range (0, 100) / 100;//false hallway*
* if (r < falseWay) {*
_ hallway = true;//it is going thru
* //Debug.Log(r + " " + i);
//we make it possible end hallway*
* r = Random.Range (0, 100) / 100;//stop hallway*
* if (r < endFalse) {}
//false hallway is made*
* else {
Transform temp = MasterPool.CreateMe (0);
Vector3 V = transform.position; //new Vector3 (transform.position.x, transform.position.y, transform.position.z);*
V = V3HallWay (V, (byte)i);_

* temp.transform.position = V;*

* Node N = temp.GetComponent ();*
_ Way = N;
//so that we start counting false ways form 0
if(trueWay){
count = 0;
}
* N.GetNewWay (this, i, false, count, hoop);
}
}
}
}
}/

* }
}
public void BuildHallway(byte way, int hoop){
if(hoop>0){
hoop–;
// way //F/B/L/R/U/D*

* //build hallway*
* if (builded && ! hallBuild) {
//BuildedHallways.Add(DebuildMe);
hallBuild = true;
//foward*

* if (hallway [0]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
//!!!check if it’s right!!!//
V = transform.position;
V.z += 3f;
//right*

* V.x += 1;
temp.transform.position = V;
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.x -= 2;
temp.transform.position = V;
temp.parent = this.transform;
//up*

temp = MasterPool.CreateMe (element * 2);
* V.x += 1;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
//down*

temp = MasterPool.CreateMe (element * 2);
* V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
if(Way[0] == null){//end hallway*

temp = MasterPool.CreateMe (element * 2+1);
* V.z += 2;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//right*

/temp = MasterPool.CreateMe (element * 2+1);
V.x += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 125, 0);
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2+1);
* V.x -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//up*

temp = MasterPool.CreateMe (element * 2+1);
* V.y += 1;
V.x -= 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//down*

temp = MasterPool.CreateMe (element * 2+1);
* V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
/}
}

* else{
Transform temp = MasterPool.CreateMe (element * 2+1);
Vector3 V = transform.position;
V.z += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
}
//back*

* if (hallway [1]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
V.z -= 3f;
//right*

* V.x += 1;
temp.transform.position = V;
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.x -= 2;
temp.transform.position = V;
temp.parent = this.transform;
//up*

temp = MasterPool.CreateMe (element * 2);
* V.x += 1;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
//down*

temp = MasterPool.CreateMe (element * 2);
* V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
if(Way[1] == null){//end hallway*

temp = MasterPool.CreateMe (element * 2 + 1);
* V.y += 1;
V.z -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
}
}
else{
Transform temp = MasterPool.CreateMe (element * 2+1);
Vector3 V = transform.position;
V.z -= 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
}
//left*

* if (hallway [2]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
V.x -= 3f;
//right*

* V.z += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.z -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//up*

temp = MasterPool.CreateMe (element * 2);
* V.z += 1;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 90);
temp.parent = this.transform;
//down*

temp = MasterPool.CreateMe (element * 2);
* V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 90);
temp.parent = this.transform;
if(Way[2] == null){//end hallway*

temp = MasterPool.CreateMe (element * 2 + 1);
* V.y += 1;
V.x -= 2;
temp.transform.position = V;
temp.parent = this.transform;
}
}
else{
Transform temp = MasterPool.CreateMe (element * 2+1);
Vector3 V = transform.position;
V.x -= 1;
temp.transform.position = V;
temp.parent = this.transform;
}
//right*

* if (hallway [3]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
V.x += 3f;
//right*

* V.z -= 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.z += 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 0);
temp.parent = this.transform;
//up*

temp = MasterPool.CreateMe (element * 2);
* V.z -= 1;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 90);
temp.parent = this.transform;
//down*

temp = MasterPool.CreateMe (element * 2);
* V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 90, 90);
temp.parent = this.transform;
if(Way[3] == null){//end hallway*

temp = MasterPool.CreateMe (element * 2 + 1);
* V.x += 2;
V.y += 1;
temp.transform.position = V;
temp.parent = this.transform;
}
}
else{
Transform temp = MasterPool.CreateMe (element * 2+1);
Vector3 V = transform.position;
V.x += 1;
temp.transform.position = V;
temp.parent = this.transform;
}
//up*

* if (hallway [4]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
V.y += 3f;
//right*

* V.x += 1;
temp.transform.position = V;
temp.transform.Rotate (90, 0, 0);
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.x -= 2;
temp.transform.position = V;
temp.transform.Rotate (90, 0, 0);
temp.parent = this.transform;
//foward*

temp = MasterPool.CreateMe (element * 2);
* V.x += 1;
V.z += 1;
temp.transform.position = V;
temp.transform.Rotate (90, 90, 0);
temp.parent = this.transform;
//back*

temp = MasterPool.CreateMe (element * 2);
* V.z -= 2;
temp.transform.position = V;
temp.transform.Rotate (90, 90, 0);
temp.parent = this.transform;
if(Way[4] == null){//end hallway*

temp = MasterPool.CreateMe (element * 2 +1);
* V.z += 1;
V.y += 2;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
}
}
else{
Transform temp = MasterPool.CreateMe (element * 2+1);
Vector3 V = transform.position;
V.y += 1;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
}
//down*

* if (hallway [5]) {
Transform temp = MasterPool.CreateMe (element * 2);
temp.parent = this.transform;
Vector3 V = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
V.y -= 3f;
//right*

* V.x += 1;
temp.transform.position = V;
temp.transform.Rotate (90, 0, 0);
temp.parent = this.transform;
//left*

temp = MasterPool.CreateMe (element * 2);
* V.x -= 2;
temp.transform.position = V;
temp.transform.Rotate (90, 0, 0);
temp.parent = this.transform;
//foward*

temp = MasterPool.CreateMe (element * 2);
* V.x += 1;
V.z += 1;
temp.transform.position = V;
temp.transform.Rotate (90, 90, 0);
temp.parent = this.transform;
//back*

temp = MasterPool.CreateMe (element * 2);
* V.z -= 2;
temp.transform.position = V;
temp.transform.Rotate (90, 90, 0);
temp.parent = this.transform;
//something wrong here don’t know what*

/for(int m=0; m<6; m++){
Debug.Log (“way 5 trues are”);
Debug.Log(Way[m] == null);
}/

* if(Way[5] == null){//end hallway*
temp = MasterPool.CreateMe (element * 2 + 1);
* V.z += 1;
V.y -= 2;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;*_

* }*
* }*
* else{*
_ Transform temp = MasterPool.CreateMe (element * 2+1);
* Vector3 V = transform.position;
V.y -= 1;
temp.transform.position = V;
temp.transform.Rotate (0, 0, 90);
temp.parent = this.transform;
}
}
}
if(Way[way] != null){
Way[way].BuildHallway(way, hoop);
}
}
public void DebuildMe(){
hallBuild = false;
//destroy all halls*

/I think it’s like this_
while(transform.childCount > 0){
Transform temp = transform.child[0];
temp.parent = null;
MasterPool.DestroyMe(temp);
_}
/

* }
public Vector3 V3HallWay (Vector3 Origin, byte way){
if (way == 0) {
Origin.z += 10;
}
else if (way == 1) {
Origin.z -= 10;
}
else if (way == 2) {
Origin.x -= 10;
}
else if (way == 3) {
Origin.x += 10;
}
else if (way == 4) {
Origin.y += 10;
}
else {
Origin.y -= 10;
}
return Origin;
}
void Start () {*_

* }*
* void OnTriggerEnter(Collider Obj){*
* if (Obj.tag == “MainCamera”) {*
* // for(int i=0; i<BuildedHallways.count; i++){*
* // BuildedHallways[0].Invoke();*
* // BuildedHallways.RemoveAt(0);*
* // }*
* //Debug.Log(correctWay);*
* if(correctWay){//!!!here we need to add more than only 2*
* GetNewWay (null, 0, true, counTrue, 2);*
* }*
* else{*
* GetNewWay (null, 0, false, countFalse, 2);*
* }*
* BuildHallway (0, 1);*
* for (byte b = 0; b < 6; b++) {*
_ if (Way != null) {_
__ Way .BuildHallway (b, 2);
** }
}
}
}
void Update () {**__

** }**
}

Recursion causes StackOverflowException, looking up StackOverflowException this is the 1st result:

 StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.

When you call a method it is loaded in memory, all the variables created are loaded in memory, then it calls itself and repeats the process as many times as needed, each time adding more memory on the stack until you run out. The method does not leave memory until it is finished running and it does not finish running until all recursive calls the finish running, so you can quickly run out of memory. Also calling it in OnTriggerEnter may have it being called multiple times.
You should try to figure out an iterative approach, or a way to reduce garbage and increase efficiency.