My game crashes when I press play in the editor after switching from scenes. I narrowed down the problem and it originates in the awake() method of my ScoreManager class when a new stack is created. How can I fix this? Here is the class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
public class ScoreManager : MonoBehaviour
{
public GUIText score;
public GUIText scoreShadow;
private int currentScore = 0;
public int scoreToUpdate = 0;
private Stack<int> stack;
private Thread t1;
private bool isDone = false;
void Awake()
{
stack = new Stack<int>();
t1 = new Thread(updateScore);
t1.Start();
}
private void updateScore()
{
while(true)
{
if(stack.Count > 0)
{
int newScore = stack.Pop();
for(int i = currentScore; i < newScore; i++){
Thread.Sleep(1); //ms
scoreToUpdate = i;
}
currentScore = scoreToUpdate;
}
if(isDone)
return;
}
}
void Update()
{
score.text = scoreToUpdate + "";
scoreShadow.text = scoreToUpdate + "";
}
public void addScore(int point)
{
stack.Push(point);
}
public void OnApplicationQuit()
{
isDone = true;
t1.Abort();
}
}