Random.Range

The way I went about it was in between what was shown, I declared the integer variable for the index then assigned it within the case 1 and case 2. The final solution I like best for a simple assignment statement.

void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        int passwordIndex;
        switch(level)
        {
            case 1:
                passwordIndex = Random.Range(0, level1Passwords.Length);
                password = level1Passwords[passwordIndex];
                break;
            case 2:
                passwordIndex = Random.Range(0, level2Passwords.Length);
                password = level2Passwords[passwordIndex];
                break;
            default:
                Debug.LogError("Invalid level number");
                break;
        }

        Terminal.WriteLine("Please enter your password: ");
    }

I also noticed in Unities documentation for Random.Range: https://docs.unity3d.com/ScriptReference/Random.Range.html

Depending on if calling the float or int versions one will be inclusive and one will be exclusive for the max argument/parameter.

1 Like

Privacy & Terms