This code is very expensive and is causing my Unity to crash, how do I prevent this?,This code is very resource intensive and causes my unity to freeze. How do I prevent this?

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

public class MathsManager : MonoBehaviour
{

private int[] index,awnser,result,multiplier;
private int operaterValue,increment;
private GameObject[] tempNumbers,tempSigns;
private float time=0.0f; 

public GameObject[] NumberArray, DragabeNumbers,PositiveNegative;
//public GameObject[] DragabeNumbers;
public Transform[] NumberSpawner, OperaterSpawner;
public BoxCollider2D[] AwnserBoxes,DraggableNumbers;
public float TimeLimit = 3.0f;
// Start is called before the first frame update
void Start()
{
    index = new int[10];
    awnser= new int[4];
    result = new int[4];
    multiplier = new int[4];
    tempNumbers = new GameObject[8];
    tempSigns = new GameObject[4];
    increment = 0;
    for (int i = 0; i < index.Length; i++)
    {
        index *= Random.Range(0, 9);*

}

for (int l = 0; l < OperaterSpawner.Length; l++)
{
operaterValue = Random.Range(0, 2);
tempSigns[l]=Instantiate(PositiveNegative[operaterValue], OperaterSpawner[l].position, Quaternion.identity);
//Debug.Log(operaterValue);
if(operaterValue==0)
{
multiplier[l] = operaterValue +1;
}
else
{
multiplier[l] = operaterValue - 2;
}

}
for (int k = 0; k < awnser.Length; k++)
{
awnser[k] = index[2 * k] + multiplier[k]*index[(2 * k) + 1];
if (k > 0)
{
while (awnser[k] == awnser[k - 1])
{
index[2 * k] = Random.Range(0, 9);
index[(2 * k) + 1] = Random.Range(0, 9);
awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
}
}
while (awnser[k] <= 0)
{
index[2 * k] = Random.Range(0, 9);
awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
}

while (awnser[k] >= 10)
{
index[2 * k] = Random.Range(0, 9);
awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
}
}
for (int j = 0; j < NumberSpawner.Length; j++)
{
tempNumbers[j]=Instantiate(NumberArray[index[j]], NumberSpawner[j].position, Quaternion.identity);
}
//Debug.Log(awnser[0]);
//Debug.Log(awnser[1]);
//Debug.Log(awnser[2]);
//Debug.Log(awnser[3]);
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < AwnserBoxes.Length; i++)
{
for (int j = 0; j < DraggableNumbers.Length; j++)
{
if(AwnserBoxes*.IsTouching(DraggableNumbers[j]))*
{
result = j;
}
}
if (awnser_==result*)
{
Debug.Log(“Yes”);
}
else
{
Debug.Log(“No”);
}
}*

time += Time.deltaTime;_

if(time>=TimeLimit)
{
ShuffleNumbers();
time = 0;
}
}

void ShuffleNumbers()
{
int temp;
temp = index[increment];
index[increment] = Random.Range(0, 9);
while(index[increment]==temp)
{
index[increment] = Random.Range(0, 9);
}
operaterValue = Random.Range(0, 2);
Destroy(tempSigns[increment/2]);
tempSigns[increment / 2] = null;
tempSigns[increment / 2] = Instantiate(PositiveNegative[operaterValue], OperaterSpawner[increment / 2].position, Quaternion.identity);
//Debug.Log(operaterValue);
if (operaterValue == 0)
{
multiplier[increment / 2] = operaterValue + 1;
}
else
{
multiplier[increment / 2] = operaterValue - 2;
}
if (increment%2==0)
{
awnser[increment / 2] = index[increment] + multiplier[increment/2] * index[increment + 1];
}
else
{
awnser[increment / 2] = index[increment-1] + multiplier[increment / 2] * index[increment];
}

for(int k = 0; k < awnser.Length; k++)
{
while (awnser[k] <= 0)
{
index[increment] = Random.Range(0, 9);
awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
}
while (awnser[k] >= 10)
{
index[increment] = Random.Range(0, 9);
awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
}
}
Destroy(tempNumbers[increment]);
tempNumbers[increment]=null;
tempNumbers[increment]=Instantiate(NumberArray[index[increment]], NumberSpawner[increment].position, Quaternion.identity);
//Debug.Log(awnser[increment/2]);

if (increment>=NumberSpawner.Length-1)
{
increment = 0;
}
else
{
increment++;
}

}
}

If it is the Start() method that takes time and freezes, you can move it to Update() and set Time.timeScale = 0 to stop the game from advancing while you do your work 1 frame at a time. You must split your loops so they return after a period of time that is less than your frame-time, to maintain your frame-rate. When done you set a flag initDone=true and do the normal Update() code. But if it’s the Update() that takes too long, you really need to reduce the amount of code you run each frame.

If your time-intensive work does not need to interact with the Unity-main-thread you can lift it out to another thread, that way you can keep the code as it is and avoid having to split the loops. It’s no fun work to split the loops, will make your code look ugly too :confused:

using System.Threading;

Thread thread;
ManualResetEvent evt = new ManualResetEvent(false);
void LoadThread()
{
    //your time intensive loop here
    //...

    //continue with next step in time intensive work...
    //evt.WaitOne();
    //...
}

ThreadStart ts = new ThreadStart(LoadThread);
thread = new Thread(ts);
thread.Priority = System.Threading.ThreadPriority.Lowest;
thread.Start();

//evt.Set(); //communicate with the thread, let it continue work

if(!thread.IsAlive) { /*thread done*/ }