mardi 9 mai 2023

Unity Infinite Loop UnityEngine.Random

I'm trying to create a loop that outputs 9 numbers in a random order with no repeating numbers. When I run it it creates an infinite loop and I can't figure out where I went wrong.

    public void randomizer()
    {
        resetArray();
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                int randomNumber = UnityEngine.Random.Range(1, 9);
                if(!numCheck(randomNumber))
                {
                    buttons[i, j] = randomNumber;
                }
                else
                {
                    j--;
                }
                Debug.Log(randomNumber);
            }
        }
    }

    public void resetArray()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                buttons[i,j] = 0;

            }
        }
    }

    public bool numCheck(int num)
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (buttons[i, j] == num)
                {
                    return true;
                }

            }
        }
        return false;
    }

The code loops infinitely, however when my friend mimic'd it in c# it worked fine on his end. What is causing the infinite loop?




Aucun commentaire:

Enregistrer un commentaire