My Unity Freeze and doesn't show any error
Hi, I'm a newbie. I tried to make a quiz game. When I answers my question in play mode, suddenly my unity freeze. I don't get this problem everytime, just sometimes. I don't know why and need to force close using task manager, because it doesn't show any errors. I tried to search topics with same problem and some of them say that you can fix it by end task adb.exe, but there's no adb.exe running in my task manager.
Here's my code using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement;
public class QuestionManager : MonoBehaviour
{
private List<Question> question;
public Text number;
public Text score;
public Text questionTxt;
public Image image;
public Text firstchoice;
public Text secondchoice;
public Text thirdchoice;
public Button firstBtn;
public Button secondBtn;
public Button thirdBtn;
public Text rightchoice;
public Button nextBtn;
private int nQuestion;
private int[] idsTemp = new int[20];
private int idsIndex;
private int[] idTaken = new int[10];
private int nIdTaken;
private int idTemp;
private string[] choicesTemp;
private string rightTemp;
private bool same;
private int nSame;
private bool answered;
private int[] answers;
private int n;
void Awake()
{
GameControl.control.NewGame();
GameControl.control.Load();
GameControl.control.ResetAnswers();
answers = new int[10];
n = 1;
nIdTaken = 0;
nQuestion = 1;
answered = false;
GameControl.control.level = 3;
GameControl.control.score = 0;
Initialize();
for (int i = 0; i < 10; i++)
{
idTaken[i] = -1;
}
image.sprite = Resources.Load<Sprite>("Sprites/null");
}
// Use this for initialization
void Start ()
{
number.transform.position = new Vector3(Screen.width * 25 / 100, Screen.height * 95 / 100, 0);
score.transform.position = new Vector3(Screen.width * 85 / 100, Screen.height * 95 / 100, 0);
questionTxt.transform.position = new Vector3(Screen.width * 33 / 100, Screen.height * 80 / 100, 0);
image.transform.position = new Vector3(Screen.width * 60 / 100, Screen.height * 55 / 100, 0);
firstBtn.transform.position = new Vector3(Screen.width * 32 / 100, Screen.height * 60 / 100, 0);
secondBtn.transform.position = new Vector3(Screen.width * 32 / 100, Screen.height * 45 / 100, 0);
thirdBtn.transform.position = new Vector3(Screen.width * 32 / 100, Screen.height * 30 / 100, 0);
rightchoice.transform.position = new Vector3(Screen.width * 61 / 100, Screen.height * 40 / 100, 0);
nextBtn.transform.position = new Vector3(Screen.width / 2, Screen.height * 10 / 100, 0);
number.text = "Question no. " + nQuestion;
score.text = "Score : " + GameControl.control.score.ToString();
gamePlay();
}
// Update is called once per frame
void Update () {
}
void LateUpdate()
{
image.SetNativeSize();
}
public void gamePlay()
{
answered = false;
same = true;
idsIndex = 0;
nSame = 0;
for (int i = 0; i < 100; i++)
{
if (GameControl.control.level == question[i].GetLevel())
{
idsTemp[idsIndex] = question[i].GetID() - 1;
idsIndex++;
}
}
while (same == true)
{
//shuffle id
Shuffle(idsTemp);
//id correction
if (nIdTaken == 0)
{
idTemp = idsTemp[0];
same = false;
}
else if(nIdTaken >= 20)
{
nIdTaken = 0;
same = false;
}
else
{
for (int i = 0; i < 10; i++)
{
if (idsTemp[0] == idTaken[i])
{
nSame++;
}
}
if (nSame == 0)
{
idTemp = idsTemp[0];
same = false;
}
}
}
idTaken[nIdTaken] = idTemp + 1;
if (nIdTaken < 9)
{
nIdTaken++;
}
//question
questionTxt.text = question[idTemp].GetQuestion().ToString();
//choices
choicesTemp = new string[question[idTemp].GetChoice().Length];
choicesTemp = question[idTemp].GetChoice();
//right choices
rightTemp = choicesTemp[question[idTemp].GetAnswer()].ToString();
//shuffle choices
Shuffle(choicesTemp);
//display choices
firstchoice.text = choicesTemp[0].ToString();
secondchoice.text = choicesTemp[1].ToString();
thirdchoice.text = choicesTemp[2].ToString();
//image
image.sprite = Resources.Load<Sprite>(question[idTemp].GetImagePath()) as Sprite;
Debug.Log("no : " + nQuestion + "idTemp : " + idTemp + ", id : " + question[idTemp].GetID() + ", level : " + GameControl.control.level);
}
public void isCorrect(Button button)
{
string answer = button.GetComponentInChildren<Text>().text.ToString();
if (answer.ToString() == (rightTemp.ToString()) && answered == false)
{
GameControl.control.score += 10;
if (GameControl.control.level < 5)
{
GameControl.control.level += 1;
}
else if(GameControl.control.level >= 5)
{
GameControl.control.level = 5;
}
answered = true;
}
else if (answer.ToString() != (rightTemp.ToString()) && answered == false)
{
if (GameControl.control.level <= 1)
{
GameControl.control.level = 1;
}
else
{
GameControl.control.level -= 1;
}
answered = true;
}
if (answered == true)
{
answers[n] = (int)GameControl.control.level;
Debug.Log(n + ", answers : " + answers[n]);
if (n < 9)
{
n++;
}
Tutorial();
}
}
public void Tutorial()
{
rightchoice.text = rightTemp.ToString();
score.text = "Score : " + GameControl.control.score.ToString();\
}
public void nextQuestion()
{
Debug.Log("masuk nextQuestion");
if (answered == true)
{
rightchoice.text = "";
if (nextBtn.IsActive() == true)
{
nQuestion += 1;
if (nQuestion == 11)
{
TotalScore();
}
else if (nQuestion < 11)
{
number.text = "Question no. " + nQuestion.ToString();
gamePlay();
}
}
}
}
public void TotalScore()
{
SaveAnswer();
SaveHS();
GameControl.control.Save();
SceneManager.LoadScene("scoreScene");
}
public void SaveAnswer()
{
GameControl.control.answer2 = answers[1];
GameControl.control.answer3 = answers[2];
GameControl.control.answer4 = answers[3];
GameControl.control.answer5 = answers[4];
GameControl.control.answer6 = answers[5];
GameControl.control.answer7 = answers[6];
GameControl.control.answer8 = answers[7];
GameControl.control.answer9 = answers[8];
GameControl.control.answer10 = answers[9];
}
public void SaveHS()
{
if (GameControl.control.hs1 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = GameControl.control.hs5;
GameControl.control.hs5 = GameControl.control.hs4;
GameControl.control.hs4 = GameControl.control.hs3;
GameControl.control.hs3 = GameControl.control.hs2;
GameControl.control.hs2 = GameControl.control.hs1;
GameControl.control.hs1 = (int)GameControl.control.score;
}
else if (GameControl.control.hs2 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = GameControl.control.hs5;
GameControl.control.hs5 = GameControl.control.hs4;
GameControl.control.hs4 = GameControl.control.hs3;
GameControl.control.hs3 = GameControl.control.hs2;
GameControl.control.hs2 = (int)GameControl.control.score;
}
else if (GameControl.control.hs3 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = GameControl.control.hs5;
GameControl.control.hs5 = GameControl.control.hs4;
GameControl.control.hs4 = GameControl.control.hs3;
GameControl.control.hs3 = (int)GameControl.control.score;
}
else if (GameControl.control.hs4 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = GameControl.control.hs5;
GameControl.control.hs5 = GameControl.control.hs4;
GameControl.control.hs4 = (int)GameControl.control.score;
}
else if (GameControl.control.hs5 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = GameControl.control.hs5;
GameControl.control.hs5 = (int)GameControl.control.score;
}
else if (GameControl.control.hs6 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = GameControl.control.hs6;
GameControl.control.hs6 = (int)GameControl.control.score;
}
else if (GameControl.control.hs7 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = GameControl.control.hs7;
GameControl.control.hs7 = (int)GameControl.control.score;
}
else if (GameControl.control.hs8 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = GameControl.control.hs8;
GameControl.control.hs8 = (int)GameControl.control.score;
}
else if (GameControl.control.hs9 < GameControl.control.score)
{
GameControl.control.hs10 = GameControl.control.hs9;
GameControl.control.hs9 = (int)GameControl.control.score;
}
else if (GameControl.control.hs10 < GameControl.control.score)
{
GameControl.control.hs10 = (int)GameControl.control.score;
}
}
static readonly System.Random rng = new System.Random();
public static void Shuffle<T>(T[] list)
{
int n = list.Length;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
void Initialize()
{
question = new List<Question>();
//question list here
}
}
Is there any wrong with my code or what? Sorry for my bad english. Thank you.
Your answer
Follow this Question
Related Questions
How do I get my character to stop moving while he is attacking? 0 Answers
unity editor freezes on play, because of script 1 Answer
Game freezes up for a second after executing code, how do I fix this? 1 Answer
Function runs fine the first time but crashes if I run it twice? 1 Answer
Unity game freezes a few seconds after loading scene 2 Answers