簡體   English   中英

if 語句即使在錯誤條件下也能執行

[英]if statements execute even with false condition

所以我對“if”塊有問題,即使條件為假,它也會執行。 方法Attack破壞目標游戲對象,它發生在通用循環的第二次迭代中,但代碼

Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);

在內部循環中工作 4 次並在控制台中顯示True,True,True,False 怎么可能? 如果條件為假但它執行代碼並顯示它為假?!

    for (int j = 1; j <= 15; j++)
        {
            mytext.text = "Round "+(j);

            for (int i = 0; i <= Activepers.Count - 1; i++)
            { 
                if (Activepers[i]!=null)
                { 
                if (Player.Contains(Activepers[i]) )

                { 
                        yield return new WaitForSeconds(1f);
                        Debug.Log(Player[i]);
                        Debug.Log(Activepers[i]);
                        Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);
                        //}


                        Activepers[i].Attack(Activepers[i], Enemy[1], () =>
                                       { });
                }
                else
                {

                        yield return new WaitForSeconds(1f);
                        Activepers[i].Attack(Activepers[i], Player[1], () =>
                                    { });
                    }

            }
            }
        }
    }

    Method Attack

    public void Attack(Characterbatle agressor, Characterbatle target, Action onAttackComplete)
        {

                Vector3 slideTargetPosition = target.Getpsition() + (Getpsition() - target.Getpsition()).normalized * 10f;
                Vector3 startingPosition = Getpsition();
                target.Damage(agressor.GetComponent<Perses>().damage, target.GetComponent<Perses>().armor,agressor.GetComponent<Perses>().chancecrit);
                if (target.healthsystem.IsDead())
                {
                **Destroy(target.gameObject);
                Destroy(target.kek.gameObject);**
            }

            SlideToPosition(slideTargetPosition, () =>
                {
                    state = State.Busy;
                    SlideToPosition(startingPosition, () =>
                    {
                        state = State.Busy;
                        onAttackComplete();
                    });
                });

在以下行中:

Debug.Log(Player.Contains(Activepers[i]) && Activepers[i] != null);

你有兩個條件: Player.Contains(Activepers[i])Activepers[i] != null ,第二個可以是false ,所以它被記錄為 false 。

你有yield return new WaitForSeconds(1f); Debug.Log之前的Debug.Log ,因此可以在運行時更改條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM