Question by
ben9902 · Feb 23, 2018 at 04:44 PM ·
infinitewhile-loop
How do i fix this infinite while loop?
hey guys, I'm struggling to fix this while loop under the InitialiseCards() function, any help would be greatly appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text matchText;
private bool initialised = false;
private int matches = 13;
void Update () {
if (!initialised)
{
InitialiseCards();
}
if (Input.GetMouseButtonUp(0))
{
CheckCards();
}
}
void InitialiseCards()
{
for (int id = 0; id < 2; id++)
{
for (int i = 1; i < 14; i++)
{
bool test = false;
int choice = 0;
while (!test)
{
choice = Random.Range(0, 26);
test = !(cards[choice].GetComponent<Card>().Initialised);
}
cards[choice].GetComponent<Card>().CardValue = i;
cards[choice].GetComponent<Card>().Initialised = true;
}
foreach(GameObject c in cards)
{
c.GetComponent<Card>().SetupGraphics();
}
if (!initialised)
{
initialised = true;
}
}
}
public Sprite GetCardBack()
{
return cardBack;
}
public Sprite GetCardFace(int i)
{
return cardFace [i - 1];
}
void CheckCards()
{
List<int> c = new List<int>();
for (int i = 0; i < cards.Length; i++)
{
if (cards[i].GetComponent<Card> ().State == 1)
{
c.Add(i);
}
}
if (c.Count == 2)
{
CardComparison(c);
}
}
void CardComparison(List<int> c)
{
Card.DO_NOT = true;
int x = 0;
if (cards[c [0]].GetComponent<Card> ().CardValue == cards[c [1]].GetComponent<Card> ().CardValue)
{
x = 2;
matches--;
matchText.text = "Number of Matches: " + matches;
if(matches == 0)
{
SceneManager.LoadScene("menu");
}
for (int i = 0; i < c.Count; i++)
{
cards[c[i]].GetComponent<Card>().State = x;
cards[c[i]].GetComponent<Card>().FalseCheck();
}
}
}
}
Comment