Another way to keep index

Hey everyone,

So I found a way to keep the variable index:

  1. Define index outside the Void Functions:
int index;
  1. Switch statement:
switch (level)
        {
            case 1:
                index = UnityEngine.Random.Range(0, passwordsLevelOne.Length);
                password = passwordsLevelOne[index]; // password is one and only one of the array
                Terminal.WriteLine("Hint: " + passwordsHintOne[index]); // hint is chosen accordingly to the password
                break;
            case 2:
                index = UnityEngine.Random.Range(0, passwordsLevelTwo.Length);
                password = passwordsLevelTwo[UnityEngine.Random.Range(0, passwordsLevelTwo.Length)];
                Terminal.WriteLine("Hint: " + passwordsHintThree[index]);
                break;
  • You need to have an array for your hints that matches the layout of the array for passwords.

I hope this helped and have a nice day :smiley:

3 Likes

Thanks for sharing!
I have found another way similar to yours; declare the integer within the function and set a value within the switch. This keeps the integer contained within the function.

    private void StartGame()
    {
        Terminal.ClearScreen();
        Terminal.WriteLine("Please enter the Password: ");
        currentScreen = Screen.Password;
        int index = 0;
        switch (level)
        {
            case 1:
                index = Random.Range(0, level1Password.Length);
                password = level1Password[index];
                break;
            case 2:
                index = Random.Range(0, level2Password.Length);
                password = level2Password[index];
                break;
            default:
                Debug.LogError("invalid level number");
                break;
        }

    }

That’s what I did as well, seems neater than not caching the variable / having a different nametype!

Did a similar thing.

void StartGame()
{
// variables
int index = 0;
currentScreen = Screen.Password;
Terminal.ClearScreen();

    // set level
    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];
            break;
        case 3:
            index = Random.Range(0, level3Passwords.Length);
            password = level3Passwords[index];
            break;
        default:
            Debug.LogError("Invalid level number");
            break;
    }

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

I did it similar to all of you! Except instead of declaring int index = 0; I use int i;

It seems work fine.


If we just initialise index once in case 1 and not again in 2 it still works fine.

Hey @Ekam_Oberoi I’m not an expert in c# yet. But this looks a bad practice to me.

In other programming languages this is not going to work.

Having said that: please check whether you have another index variable outside of your StarGame function.

yeah that was my solution too

Privacy & Terms