Pasting the random number generator to get the index of a random password seems a bit…ugly? I get what you’re saying in the course that setting the index at the top causes problems, but what if you only initialise the variable? This is what I have…
void StartGame ()
{
int index;
// Other code omitted for simplicity's sake...
switch (level)
{
case 1:
index = Random.Range(0, level1Passwords.Length);
password = level1Passwords[index];
break;
case 2:
index = Random.Range(0, level2Passwords.Length);
password = level2Passwords[index];
default:
Debug.LogError("Invalid level number");
}
}
For me, it’s much more readable to use the index variable, but I’m wondering if there are any issues with doing it like this? I don’t seem to have any but as said in the course video, it’s a potentially nasty bug as it’s difficult to come across, but the potential is there.