当一切看起来都很好时,条件不起作用

美好的一天,我的 Unity 游戏中有一个脚本,它创建一个列表并将其数字顺序随机化,然后将值传递给另一个列表以检查一些特定属性,这是代码:


// Use this for initialization

private List<int> PreList = new List<int>();

private List<int> ButtonList = new List<int>();

private List<int> UserList = new List<int>();

private System.Random Rnd = new System.Random();


void Randomizer()

{

    PreList.Add(1);

    PreList.Add(2);

    PreList.Add(3);

    PreList.Add(4);

    PreList.Add(5);

    PreList.Add(6);

    PreList.Add(7);

    PreList.Add(8);

    PreList.Add(9);

    PreList = PreList.OrderBy(C => Rnd.Next()).ToList();

    foreach (int Number in PreList)

    { 

        Debug.Log(Number);

        Debug.Log(ButtonList.Count);

        if (Number == 1)

        {

            OneMethod();

        }

        else if (Number == 2)


        {

            TwoMethod();

        }

        else if (Number == 3)


        {

            ThreeMethod();

        }

        else if (Number == 4)


        {

            FourMethod();

        }

        else if (Number == 5)


        {

            FiveMethod();

        }

        else if (Number == 6)


        {

            SixMethod();

        }

        else if (Number == 7)


        {

            SevenMethod();

        }

        else if (Number == 8)


        {

            EightMethod();

        }

        else if (Number == 9)


        {

            NineMethod();

        }

    }

}

    void OneMethod()

    {

        ButtonList.Add(1);

        GameObject RedButton = GameObject.Find("Red"); 

//There are 8 methods just like this, but it variates some stuff like the name and the number, all of these add numbers to ButtonList

    }

此时,输出控制台只是说 ButtonList 的计数是 9,但是,如果我放一个 if 来检查它,它永远不会将值设为 true,就像它不执行方法并且 ifs 永远不会运行,但是,你知道为什么吗?


森栏
浏览 147回答 1
1回答

万千封印

我不确定这是否会解决您的问题,但这是生成随机顺序列表的更好方法:public class MyClass {&nbsp; &nbsp; private List<int> PreList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };&nbsp; &nbsp; private List<int> ButtonList = new List<int>();&nbsp; &nbsp; private List<int> UserList = new List<int>();&nbsp; &nbsp; void Randomizer() {&nbsp; &nbsp; &nbsp; &nbsp; while (PreList.Count > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var idx = UnityEngine.Random.Range(0, PreList.Count); // Randomly select from remaining items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var value = PreList[idx]; // Get item value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreList.RemoveAt(idx); // Remove item from future options&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ButtonList.Add(value); // Add to end of 'randomised' list&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach (var value in ButtonList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoSomethingWith(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void DoSomethingWith(int value) {&nbsp; &nbsp; &nbsp; &nbsp; switch(value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1: OneMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2: TwoMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3: ThreeMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 4: FourMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 5: FiveMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 6: SixMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 7: SevenMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 8: EightMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 9: NineMethod(); break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}编辑:添加示例使用 DoSomething()
打开App,查看更多内容
随时随地看视频慕课网APP