Unity继续崩溃

时间:2016-12-27 19:04:23

标签: c# unity3d

确定我修复了错误,谢谢你们帮助了但是现在Unity在按下运行按钮的时候很多人因为循环而说很多人说但是我真的不知道如何解决它

GameManager.cd

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class GameManager : MonoBehaviour {

public Sprite[] cardFace;
public Sprite cardBack;
public  GameObject[] cards;
public Text macthText;

private bool _init = false;
private int _matches = 13;



// Update is called once per frame
void Update () {


    if (!_init)
        initializecards ();

    if (Input.GetMouseButtonUp (0))
        checkCards ();

}

void initializecards(){

    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, 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> ().setupGrapgics ();

    if (!_init)
        _init = true;

}

public Sprite getCardBack(){
    return cardBack;
    }

public Sprite getCardface(int i){

    return cardFace [i-1];
}

public 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--;
        macthText.text = "Number of Macthes: " + _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 ();

    }


    }
  }

card.cd

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Card : MonoBehaviour {

public static bool DO_NOT = false;

[SerializeField]
private int state;
[SerializeField]
private int cardValue;
[SerializeField]
private bool initialized = false;

private Sprite cardBack;
private Sprite cardFace;

private GameObject manager;

void start() {
    state = 0;
    manager = GameObject.FindGameObjectWithTag ("Manager");

}

public void setupGrapgics() {

    cardBack = manager.GetComponent<GameManager> ().getCardBack ();
    cardFace = manager.GetComponent<GameManager> ().getCardface     (cardValue);

    flipCard ();

}

public void flipCard() {

    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; }


}

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;

}
}

先谢谢

1 个答案:

答案 0 :(得分:0)

以下代码会冻结游戏,因为test始终是false而永远不会true

while (!test) {
choice = Random.Range (0, cards.Length);
test = !(cards [choice].GetComponent<Card> ().Initialized);
}

追踪到:

public bool Initialized {
    get { return initialized; }
    set { Initialized = value; }
}

因为set { Initialized = value; }应为set { initialized = value; }

注意:

在使用生成的数字作为变量退出循环时,不要在while循环中生成随机数。如果失败,您将冻结Unity。在协程函数中执行此操作会很好,然后将yield return null;放在其末尾。这永远不会冻结Unity。

while (!test) {
choice = Random.Range (0, cards.Length);
test = !(cards [choice].GetComponent<Card> ().Initialized);
yield return null;
}