c# –Unity在Play上崩溃,很可能是无限循环,但找不到问题

c# –Unity在Play上崩溃,很可能是无限循环,但找不到问题,第1张

概述谢谢阅读.我目前正在使用C#在Unity中构建一个小型存储卡游戏.我已经完成了代码的主要部分但是当我按某个场景上的播放按钮时,Unity会冻结.我相信这是由于一个无限的While循环,但我找不到问题.我真的很感激任何人都可以提供帮助.我将在下面留下我的代码.提前致谢.usingSystem.C

谢谢阅读.

我目前正在使用C#在Unity中构建一个小型存储卡游戏.我已经完成了代码的主要部分但是当我按某个场景上的播放按钮时,Unity会冻结.
我相信这是由于一个无限的While循环,但我找不到问题.我真的很感激任何人都可以提供帮助.我将在下面留下我的代码.提前致谢.

using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using UnityEngine.SceneManagement;using UnityEngine;public class Pairs : MonoBehavIoUr {    public Sprite[] face; //array of card faces    public Sprite back;    public GameObject[] deck; //array of deck    public Text pairsCount;    private bool deckSetUp = false;    private int pairsleft = 13;    // Update is called once per frame    voID Update () {        if (!deckSetUp)        {            SetUpDeck();        }        if (input.GetMousebuttonUp(0)) //detects left click        {            CheckDeck();        }    }//Update    voID SetUpDeck()    {        for (int ix = 0; ix < 2; ix++)         {            for(int i = 1; i < 14; i++)//sets up card value (2-10 JQKA)            {                bool test = false;                int val = 0;                while (!test)                {                   val = Random.Range(0, deck.Length);                   test = !(deck[val].GetComponent<Card>().SetUp);                }//while                //sets up cards                deck[val].GetComponent<Card>().Number = i;                deck[val].GetComponent<Card>().SetUp = true;            }//nested for        }//for        foreach (GameObject crd in deck)        {            crd.GetComponent<Card>().setUpArt();        }        if (!deckSetUp)        {            deckSetUp = true;        }    }//SetUpDeck    public Sprite getBack()    {        return back;    }//getBack    public Sprite getFace(int i)    {        return face[i - 1];    }//getFace    voID CheckDeck()    {        List < int > crd = new List<int>();        for(int i = 0; i < deck.Length; i++)        {            if(deck[i].GetComponent<Card>().State == 1)            {                crd.Add(i);            }        }        if(crd.Count == 2)        {            CompareCards(crd);        }    }//CheckDeck    voID CompareCards(List<int> crd)    {        Card.NO_TURN = true; //stops cards turning        int x = 0;        if(deck[crd[0]].GetComponent<Card>().Number ==   deck[crd[1]].GetComponent<Card>().Number)        {            x = 2;            pairsleft--;            pairsCount.text = "PAirs REMAINING: " + pairsleft;            if(pairsleft == 0) // goes to home screen when game has been won            {                SceneManager.LoadScene("Home");            }        }        for(int j = 0; j < crd.Count; j++)        {            deck[crd[j]].GetComponent<Card>().State = x;            deck[crd[j]].GetComponent<Card>().PairCheck();        }    }//CompareCards}

我相信问题在于while(!test),但我不知道为什么测试永远不会成真.

using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using UnityEngine;public class Card : MonoBehavIoUr {    public static bool NO_TURN = false;     [SerializefIEld]    private int cardState; //state of card    [SerializefIEld]    private int cardNumber; //Card value (1-13)    [SerializefIEld]    private bool _setUp = false;    private Sprite back; //card back (Green square)    private Sprite face; //card face (1-10 JQKA)    private GameObject pairsManager;    voID Begin()    {        cardState = 1; //cards face down        pairsManager = GameObject.FindGameObjectWithTag("PairsManager");     }    public voID setUpArt()    {        back = pairsManager.GetComponent<Pairs>().getBack();        face = pairsManager.GetComponent<Pairs>().getFace(cardNumber);        turnCard();//turns the card    }    public voID turnCard() //handles turning of card    {        if (cardState == 0)        {            cardState = 1;        }        else if(cardState == 1)        {            cardState = 0;        }        if (cardState == 0 && !NO_TURN)        {            GetComponent<Image>().sprite = back; // shows card back        }        else if (cardState == 1 && !NO_TURN)        {            GetComponent<Image>().sprite = face; // shows card front        }    }    //setters and getters    public int Number    {        get {return cardNumber;}        set { cardNumber = value;}    }    public int State    {        get { return cardState; }        set { cardState = value; }    }    public bool SetUp    {        get { return _setUp; }        set { _setUp = value; }    }    public voID PairCheck()     {        StartCoroutine(pause ());    }    IEnumerator pause()    {        yIEld return new WaitForSeconds(1);         if (cardState == 0)        {            GetComponent<Image>().sprite = back;        }        else if (cardState == 1)        {            GetComponent<Image>().sprite = face;        }    }}

感谢您的阅读,如果有帮助,我会发布一个指向github存储库的链接.
github repository

解决方法:

您的deck数组中至少有一张卡,其中_setUp设置为true,这将使其进入无限循环.

它进入无限循环的原因是因为它将所有可用的_setUp设置为true,并且它将继续寻找设置为false的_setUp并且它永远不会找到任何.

你需要至少26个具有_setUp为false的对象的原因是因为在嵌套的for循环中你循环13次然后你做了两次,总共有26个循环.所以你需要至少26个对象.

你可以做些什么来确保它们都是假的是在进入for循环之前将它们全部设置为false

for(int i = 0; i < deck.Length; i++){    deck[i].GetComponent<Card>().SetUp = false;} 
总结

以上是内存溢出为你收集整理的c# – Unity在Play上崩溃,很可能是无限循环,但找不到问题全部内容,希望文章能够帮你解决c# – Unity在Play上崩溃,很可能是无限循环,但找不到问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存