C# 统一游戏冻结
我有这段代码,但当我试图运行它时,Unity冻结了,我是否有一个看不到的无限循环C# 统一游戏冻结,c#,unity3d,freeze,C#,Unity3d,Freeze,我有这段代码,但当我试图运行它时,Unity冻结了,我是否有一个看不到的无限循环 using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using System.Collections.Generic; public class GameManager : MonoBehaviour { public Sprite[] card
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public Sprite[] cardFace;
public Sprite cardBack;
public GameObject[] cards;
public Text matchText;
private bool _init = false;
private int _matches = 13;
public int i;
// Update is called once per frame
void Update () { //basically what it does is it doesn't hit the update until everything is loaded
if (!_init)
initializeCards ();
if (Input.GetMouseButtonUp(0)) //player has left clicked
checkCards();
}
void initializeCards()
{
for (int id = 0; id <2; id++)
{
for (i = 1; i < 14; i++)
{
bool test = false;
int choice = 0;
while (!test)
{
choice = Random.Range(0, cards.Length);
test = !(cards [choice].GetComponent<card>().initialized);
}
cards[choice].GetComponent<card>().cardValue = i;
cards[choice].GetComponent<card>().initialized = true;
}
}
foreach (GameObject c in cards)
c.GetComponent<card>().setupGraphics();
if (!_init)
_init = true;
}
public Sprite getCardBack()
{
return cardBack;
}
public Sprite getCardFace()
{
return cardFace[i - 1];
}
void checkCards()
{
List<int> c = new List<int>();
for (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("bedroomscene");
}
for (int i = 0; i < c.Count; i++){
cards[c[i]].GetComponent<card>().state = x;
cards[c[i]].GetComponent<card>().falseCheck();
}
}
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.SceneManagement;
使用系统集合;
使用System.Collections.Generic;
公共类GameManager:MonoBehavior{
公共精灵[]卡片脸;
公共雪碧卡背;
公共游戏对象[]卡;
公共文本匹配文本;
private bool_init=false;
私有int_匹配=13;
公共国际一级;
//每帧调用一次更新
void Update(){//基本上,它在加载所有内容之前不会命中更新
如果(!\u init)
初始化cards();
if(Input.GetMouseButtonUp(0))//播放器已左键单击
支票卡();
}
void initializeCards()
{
对于(int id=0;id您是否确定已初始化的是否为假至少一张卡的组件数组?
否则,它将不会从此循环中断:
另外,由于选项的值是随机的。它也不确定是否会中断while循环
while (!test)
{
choice = Random.Range(0, cards.Length);
test = !(cards [choice].GetComponent<card>().initialized);
}
while(!test)
{
选择=随机范围(0,卡片长度);
测试=!(卡[choice].GetComponent().initialized);
}
可能是因为while循环。通常,尝试生成所有卡的列表,然后“随机选择一张”这是一个非常糟糕的主意。更好的方法是生成一张牌组,然后对其随机排序一次,然后按顺序拉牌。或者,创建一张包含所有牌的牌组,随机选择一张牌,然后将其从牌组中移除,以便下一次随机选择少选一张牌。