Pazl Game
Pazl Game

Reputation: 71

Conditional not working when all looks fine

Good day, I have a script in my Unity game, it creates a list and it randomize its numbers order, then, the value passes to another list to check some specific properties, this is the code:

// 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
    }

At this moment, the output console just says that the count of the ButtonList is 9, but, if i put an if to check that, it never gets the value to true, is like it doesnt execute the methods and the ifs never runs, but, do you have any idea of why?

Upvotes: 2

Views: 86

Answers (1)

Immersive
Immersive

Reputation: 1704

I'm not sure if this will fix your problem, but here's a better way of generating random-order lists:

public class MyClass {
    private List<int> PreList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    private List<int> ButtonList = new List<int>();
    private List<int> UserList = new List<int>();

    void Randomizer() {
        while (PreList.Count > 0 ) {
            var idx = UnityEngine.Random.Range(0, PreList.Count); // Randomly select from remaining items
            var value = PreList[idx]; // Get item value
            PreList.RemoveAt(idx); // Remove item from future options
            ButtonList.Add(value); // Add to end of 'randomised' list
        }

        foreach (var value in ButtonList) {
            DoSomethingWith(value);
        }
    }

    void DoSomethingWith(int value) {
        switch(value) {
            case 1: OneMethod(); break;
            case 2: TwoMethod(); break;
            case 3: ThreeMethod(); break;
            case 4: FourMethod(); break;
            case 5: FiveMethod(); break;
            case 6: SixMethod(); break;
            case 7: SevenMethod(); break;
            case 8: EightMethod(); break;
            case 9: NineMethod(); break;
        }
    }
}

Edit: Added example use of DoSomething()

Upvotes: 1

Related Questions