1. <tfoot id='Sh7Pi'></tfoot>
      <i id='Sh7Pi'><tr id='Sh7Pi'><dt id='Sh7Pi'><q id='Sh7Pi'><span id='Sh7Pi'><b id='Sh7Pi'><form id='Sh7Pi'><ins id='Sh7Pi'></ins><ul id='Sh7Pi'></ul><sub id='Sh7Pi'></sub></form><legend id='Sh7Pi'></legend><bdo id='Sh7Pi'><pre id='Sh7Pi'><center id='Sh7Pi'></center></pre></bdo></b><th id='Sh7Pi'></th></span></q></dt></tr></i><div id='Sh7Pi'><tfoot id='Sh7Pi'></tfoot><dl id='Sh7Pi'><fieldset id='Sh7Pi'></fieldset></dl></div>
      • <bdo id='Sh7Pi'></bdo><ul id='Sh7Pi'></ul>
      <legend id='Sh7Pi'><style id='Sh7Pi'><dir id='Sh7Pi'><q id='Sh7Pi'></q></dir></style></legend>
    2. <small id='Sh7Pi'></small><noframes id='Sh7Pi'>

      Unity 在播放时崩溃,很可能是无限的 While 循环,但找不到问题

      时间:2023-05-22

      <legend id='ciden'><style id='ciden'><dir id='ciden'><q id='ciden'></q></dir></style></legend>
      <i id='ciden'><tr id='ciden'><dt id='ciden'><q id='ciden'><span id='ciden'><b id='ciden'><form id='ciden'><ins id='ciden'></ins><ul id='ciden'></ul><sub id='ciden'></sub></form><legend id='ciden'></legend><bdo id='ciden'><pre id='ciden'><center id='ciden'></center></pre></bdo></b><th id='ciden'></th></span></q></dt></tr></i><div id='ciden'><tfoot id='ciden'></tfoot><dl id='ciden'><fieldset id='ciden'></fieldset></dl></div>

      <small id='ciden'></small><noframes id='ciden'>

      • <tfoot id='ciden'></tfoot>
            <bdo id='ciden'></bdo><ul id='ciden'></ul>

              <tbody id='ciden'></tbody>

                本文介绍了Unity 在播放时崩溃,很可能是无限的 While 循环,但找不到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                感谢阅读.

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

                I'm currently building a small memory card game in Unity using C#. I have the main portion of code finished but when I press the play button on a certain scene Unity freezes. I believe it is due to an infinite While loop, but I can not find the issue. I would really appreciate any help anyone can offer. I will leave my code below. Thanks in advance.

                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) 但我不知道为什么 test 永远不会成为真的.

                I believe the issue lies in the while(!test) but i do not know why test never become true.

                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 存储库

                Thank you for reading, I will post a link to the github repository if that helps. github repository

                推荐答案

                你的套牌数组中至少有一张卡片,其中 _setUp 设置为 true 这将使它进入一个无限循环.

                Your deck array has at least one card in it that has _setUp set to true which would make it go in a infinite loop.

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

                The reason it goes in a infinite loop is because it will have set all available _setUp to true and it would keep looking for _setUp that are set to false and it will never find any.

                您需要至少 26 个具有 _setUpfalse 的对象的原因是因为在嵌套的 for 循环中,您循环了 13 次,然后您执行了两次,这给出了总共26个循环.所以你至少需要 26 个对象.

                The reason you need at least 26 object that have _setUp to false is because in the nested for loop you loop 13 times and then you do that twice which gives a total of 26 loops. So you need at least 26 objects.

                为了确保它们都是false,你可以做的是在进入for循环之前将它们全部设置为false

                What you can do to make sure that they're all false is to set them all to false before entering the for loop

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

                这篇关于Unity 在播放时崩溃,很可能是无限的 While 循环,但找不到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:RichTextBox 从文本中删除转义字符 下一篇:使用 Visual Studio 2017.3 编译 Unity UWP 构建时出错

                相关文章

                <i id='y5qnj'><tr id='y5qnj'><dt id='y5qnj'><q id='y5qnj'><span id='y5qnj'><b id='y5qnj'><form id='y5qnj'><ins id='y5qnj'></ins><ul id='y5qnj'></ul><sub id='y5qnj'></sub></form><legend id='y5qnj'></legend><bdo id='y5qnj'><pre id='y5qnj'><center id='y5qnj'></center></pre></bdo></b><th id='y5qnj'></th></span></q></dt></tr></i><div id='y5qnj'><tfoot id='y5qnj'></tfoot><dl id='y5qnj'><fieldset id='y5qnj'></fieldset></dl></div>

                <tfoot id='y5qnj'></tfoot>
                <legend id='y5qnj'><style id='y5qnj'><dir id='y5qnj'><q id='y5qnj'></q></dir></style></legend>
              1. <small id='y5qnj'></small><noframes id='y5qnj'>

                • <bdo id='y5qnj'></bdo><ul id='y5qnj'></ul>