(C#) 当我执行这个方法时,Unity会突然关闭,所以我无法提供错误代码。

huangapple go评论69阅读模式
英文:

(C#) When I play this Method, Unity Just Shut Down So I cannot provide you to Error Code

问题

  1. 游戏中敌人的生命值达到0时,调用uiSelectUnit.Show()。
   void Update()
    {
        if (!isLive)
            return;

        gameTime += Time.deltaTime;

        if (hasAllEnemiesSpawned && gameTime > 1.0f && enemies.Count == 0)
        {
            uiLevelUp.Show();
        }
    }
  1. 当调用Show()时,激活UI窗口,暂停游戏,并调用Next()。
    public void Show()
    {
        Next();
        rect.localScale = Vector3.one;
        GameManager.instance.Stop();
    }
  1. 选择三个不同的索引,为每个索引的UnitCard/Draggable组件分配数据并激活卡片。
 void Next()
    {
        foreach (UnitCard unitCard in unitCards)
        {
            unitCard.gameObject.SetActive(false);
        }

        int[] ran = new int[3];
        while (true)
        {
            ran[0] = Random.Range(0, unitDataItems.Length);
            ran[1] = Random.Range(0, unitDataItems.Length);
            ran[2] = Random.Range(0, unitDataItems.Length);

            if (ran[0] != ran[1] && ran[1] != ran[2] && ran[0] != ran[2])
                break;
        }

        for (int index = 0; index < ran.Length; index++)
        {
            UnitData ranUnitData = unitDataItems[ran[index]];
            UnitCard ranItem = unitCards[index];
            ranItem.GetComponent<UnitCard>().data = ranUnitData;
            ranItem.GetComponent<Draggable>().linkedUnitData = ranUnitData;
            ranItem.gameObject.SetActive(true);
        }
    }

    private UnitData[] LoadUnitDataFromResources()
    {
        Object[] loadedUnitData = Resources.LoadAll("Unit/Faction1", typeof(UnitData));
        UnitData[] unitDataItems = new UnitData[loadedUnitData.Length];

        for (int i = 0; i < loadedUnitData.Length; i++)
        {
            unitDataItems[i] = (UnitData)loadedUnitData[i];
        }

        return unitDataItems;
    }

在按照上述步骤进行操作并启动游戏后,当调用uiSelectUnit.Show()并执行Next时,Unity突然崩溃并游戏停止... 我想告诉您错误数据,但由于它突然停止,我无法找出原因... 如果您需要完整的脚本来解决此问题,请告诉我。展示三个随机选择的单位,并允许用户选择所需单位。

英文:

First off, I gonna Discribe you to Script I'll trying to.

  1. When the enemy's health reaches 0 in the game, call uiSelectUnit.Show().
   void Update()
    {
        if (!isLive)
            return;

        gameTime += Time.deltaTime;

        if (hasAllEnemiesSpawned &amp;&amp; gameTime &gt; 1.0f &amp;&amp; enemies.Count == 0)
        {
            uiLevelUp.Show();
        }
    }

2.When Show() is Called, it activates the UI Window, pause the Game, and calls Next().

    public void Show()
    {
        Next();
        rect.localScale = Vector3.one;
        GameManager.instance.Stop();
    }

(+ There seem to be no issues up to step 2. There were no anomalies when tested with other scripts.)

  1. Select three different indexes, assign data to the UnitCard/Draggable component of each index, and activate the cards.
 void Next()
    {
        foreach (UnitCard unitCard in unitCards)
        {
            unitCard.gameObject.SetActive(false);
        }

        int[] ran = new int[3];
        while (true)
        {
            ran[0] = Random.Range(0, unitDataItems.Length);
            ran[1] = Random.Range(0, unitDataItems.Length);
            ran[2] = Random.Range(0, unitDataItems.Length);

            if (ran[0] != ran[1] &amp;&amp; ran[1] != ran[2] &amp;&amp; ran[0] != ran[2])
                break;
        }

        for (int index = 0; index &lt; ran.Length; index++)
        {
            UnitData ranUnitData = unitDataItems[ran[index]];
            UnitCard ranItem = unitCards[index];
            ranItem.GetComponent&lt;UnitCard&gt;().data = ranUnitData;
            ranItem.GetComponent&lt;Draggable&gt;().linkedUnitData = ranUnitData;
            ranItem.gameObject.SetActive(true);
        }
    }

    private UnitData[] LoadUnitDataFromResources()
    {
        Object[] loadedUnitData = Resources.LoadAll(&quot;Unit/Faction1&quot;, typeof(UnitData));
        UnitData[] unitDataItems = new UnitData[loadedUnitData.Length];

        for (int i = 0; i &lt; loadedUnitData.Length; i++)
        {
            unitDataItems[i] = (UnitData)loadedUnitData[i];
        }

        return unitDataItems;
    }

After entering the steps as mentioned above and starting the game, when uiSelectUnit.Show() was called and Next was executed, Unity suddenly crashed and the game stopped... I'd like to tell you the error data, but I can't figure out the cause since it suddenly stopped... I'd really appreciate any help!

the data was managed as follows.
enter image description here
(The SelectUnit script is applied to the SelectUnit object, with Cards as its children connected to UnitCard. Data is set to None, as it is designed to connect automatically.)

If you need to full script to solve this Problem, Just tell me.

Display three randomly selected units, and allow the user to choose the desired unit.

答案1

得分: 1

崩溃可能是由函数Next中的while循环引起的,该循环永远不会结束:只有当rand[0]rand[1]rand[2]不同的时候循环才会结束,但是如果在数组unitDataItems中的项不多,可能这个条件永远不会成立,循环会一直继续下去,从而导致Unity崩溃。

英文:

The crash may be caused by the while loop in the function Next that never ends: the loop ends only if rand[0], rand[1] and rand[2] are different but, if there aren't many items in the array unitDataItems, it's possible that that condition never becomes true and the loop continues forever, thus crashing Unity.

huangapple
  • 本文由 发表于 2023年5月7日 22:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定