- Home /
Unity freezes every time I run one specific scene
I have different scenes that work fine except when I reach one specific scene. The different thing about this scene is the scripts. A game manager script and a card script. (The game is a personnalized memory game based on the memory card game). Every time I reach that scene, Unity freezes and I can't play it nor load another scene (using a loadonclick script). I am new to Unity and this is the first real problem I found. Especially when this same scene used to work fine when I first finished it yesterday... Anyway, here is the script I am currently using
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class CardScript : MonoBehaviour {
public static bool DO_NOT = false;
[SerializeField]
private int _state;
[SerializeField]
private int _cardValue;
private bool _initialized = false;
private Sprite _cardBack;
private Sprite _cardFace;
private GameObject _manager;
void Start() {
_state = 1;
_manager = GameObject.FindGameObjectWithTag("Manager");
}
public void setUpGraphics() {
_cardBack = _manager.GetComponent<GameManager>().getCardBack();
_cardFace = _manager.GetComponent<GameManager>().getCardFace(_cardValue);
flipCard();
}
public void flipCard() {
if (_state == 0)
_state = 1;
else if (_state == 1)
_state = 0;
if (_state == 0 && !DO_NOT)
GetComponent<Image> ().sprite = _cardBack;
else if (_state == 1 && !DO_NOT)
GetComponent<Image> ().sprite = _cardFace;
}
public int cardValue {
get { return _cardValue; }
set { _cardValue = value; }
}
//50
public int state {
get { return _state; }
set { _state = value; }
}
public bool initialized {
get { return _initialized; }
set { _initialized = value; }
}
public void falseCheck() {
StartCoroutine(pause());
}
IEnumerator pause() {
yield return new WaitForSeconds(1);
if (_state == 0)
GetComponent<Image>().sprite = _cardBack;
else if (_state == 1)
GetComponent<Image>().sprite = _cardFace;
DO_NOT = false;
}
}
The part that I changed a lot lately was the flipcard part because I had some trouble getting it to work, so I'm guessing the problem could be coming from there?
in my other script, inside the initializecards function
void initializeCards() { for (int id = 0; id <2; id++) { for(int i = 1; i < 7; i++) {
bool test = false;
int choice = 0;
while (!test) {
choice = Random.Range(0,cards.Length);
test = !(cards [choice].GetComponent <CardScript>().initialized);
}
cards[choice].GetComponent<CardScript>().cardValue = i;
cards[choice].GetComponent<CardScript>().initialized = true;
}
}
foreach (GameObject c in cards)
c.GetComponent<CardScript>().setUpGraphics();
if (!_init) {
_init = true;
}
}
Does your test bool in your while loop ever get set to true? If not this means you're in an infinite loop which will crash your application.
Answer by Layas · Mar 20, 2017 at 04:41 PM
Solved: The problem was inside the 2nd for loop. i was set from 0 to 7 covering 12 cards whereas in my scene, I had changed it to 16 and forgot to modify the range of i in the script from 0 to 9.