Tried to make it so you can "unlock" level 2 and 3

Hey guys,
So I might have made my game too complicated. I tried to make it so you had to unlock a password (Put in my code as levelkey so as not to be confused with password or level1passwords) that enables you to access that level.

This is my arrays and Game State stuff… n.n

    //Game configuration data
    string[] level1Passwords = { "carrot", "plum", "melon", "kale", "yam" };
    string[] level2Passwords = { "chicken breast", "chick pea ", "pepperoni", "veal chops", "octopus" };
    string[] level3Passwords = { "cholesterol", "malignant tumor", "genetic disease", "vaccination", "tuberculosis", "terminal illness", };

    string[] levelkeys = { "intermediatepass", "i_luv4n4gr4ms", };



    //Game State
    int level;
    enum Screen { MainMenu, Password, Win, LevelKey };
    Screen currentScreen;
    string password;
    string levelkey;

I tried to make it so that when you’re on the mainmenu, if level = 2 or 3 (i.e. if the input is 2 or 3 from the main menu,) then you are directed to AskForLevelKey, and the screen is switched to Screen.LevelKey.
From here, I want there to be new prompts to the player to enter the levelkey, I put these prompts in AskForLevelKey.

My only compiler errors are:
Assets/Hacker.cs(130,28): error CS0103: The name leve1keys' does not exist in the current context Assets/Hacker.cs(133,28): error CS0103: The nameleve1keys’ does not exist in the current

Let me know if there’s any info/clarification needed!!!

Huge virtual hugs to anyone who can help me out!!!

Thank you so much

contextAna’s Gram 2 W_Level_Key.zip (8.8 MB)

note: I called my game Ana’s Gram instead of Terminal Hacker, I don’t think the backstory on the game or my “theme” is necessary to answer my question, though when I’m done and I’ve shared my creation I’ll link it to this page for people to see!

update: I fixed the compiler error! It was written as leve1keys instead of levelkeys on line 130 and 133.

Still can’t quite get it so that you have to make the person to enter the levelkey to progress to the next level.

Hi Jacob,

Could you share the whole script here, please?

Hi Nina,
I shared a zip file to it but yes I’ll share the whole script!

using UnityEngine;

public class Hacker : MonoBehaviour
{
    //Game configuration data
    string[] level1Passwords = { "carrot", "plum", "melon", "kale", "yam" };
    string[] level2Passwords = { "chicken breast", "chick pea ", "pepperoni", "veal chops", "octopus" };
    string[] level3Passwords = { "cholesterol", "malignant tumor", "genetic disease", "vaccination", "tuberculosis", "terminal illness", };

    string[] levelkeys = { "intermediatepass", "i_luv4n4gr4ms", };



    //Game State
    int level;
    enum Screen { MainMenu, Password, Win, LevelKey };
    Screen currentScreen;
    string password;
    string levelkey;

    // Start is called before the first frame update
    void Start()
    {
        ShowMainMenu("Hello citizen 8395729489");
    }

    void ShowMainMenu(string greeting)
    {
        Terminal.ClearScreen();
        currentScreen = Screen.MainMenu;
        Terminal.WriteLine(greeting);
        Terminal.WriteLine("Prove your worth to us.");
        Terminal.WriteLine("Answer the puzzle and your reward");
        Terminal.WriteLine("will be your survival.");
        Terminal.WriteLine("Press 1 for the Preliminary test.");
        Terminal.WriteLine("Press 2 for the Intermediate test.");
        Terminal.WriteLine("Press 3 for the Advanced test.");
        Terminal.WriteLine("Type menu to leave.");
        Terminal.WriteLine("Enter your selection.");
    }


    private void RunMainMennu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            if (level == 2 || level == 3)
            {
                AskForLevelKey();
            }
            else if (level == 1)
            {
                AskForPassword();
            }
            
        }
        else if (isValidLevelNumber)
        {
            level = int.Parse(input);
            AskForPassword();
        }
        else if (input == "exit")
        {
            YouCantLeave("Welcome back citizen 8395729489");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid level.");
        }
    }


    void YouCantLeave(string greeting)
    {
        Terminal.ClearScreen();
        Terminal.WriteLine(greeting);
        Terminal.WriteLine("Just kidding, you can't leave.");
        Terminal.WriteLine("Prove your worth to us, ");
        Terminal.WriteLine("type menu to return to the Main Menu.");
    }



    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu("Welcome back citizen 8395729489");
        } // TODO handle differently depending on screen
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMennu(input);
        }
        else if (currentScreen == Screen.LevelKey)
        {
            CheckLevelKey(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }

    }

    void CheckLevelKey(string input)
    {
        if (input == levelkey)
        {
            DisplayWinScreen();
        }
        else
        {
            AskForPassword();
        }
    }

    void CheckPassword(string input)
    {
        if (input == password)
        {
            DisplayWinScreen();
        }
        else
        {
            AskForPassword();
        }
    }

    void AskForLevelKey()
    {
        currentScreen = Screen.LevelKey;
        Terminal.ClearScreen();
        switch (level)
        {
            case 2:
                levelkey = levelkeys[0];
                break;
            case 3:
                levelkey = levelkeys[1];
                break;
            default:
                Debug.LogError("That isn't a level, you stupid human.");
                break;
        }

        if (level == 2)
        {
            Terminal.WriteLine("you have selected the intermediate test");
            Terminal.WriteLine("please enter the password required to access the intermediate test.");
        }
        else if (level == 3)
        {
            Terminal.WriteLine("you have selected the advanced test");
            Terminal.WriteLine("please enter the password required to access the advanced test.");
        }
    }

    void AskForPassword()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                password = level1Passwords[Random.Range(0, level1Passwords.Length)];
                break;
            case 2:
                password = level2Passwords[Random.Range(0, level2Passwords.Length)];
                break;
            case 3:
                password = level3Passwords[Random.Range(0, level3Passwords.Length)];
                break;
            default:
                Debug.LogError("That isn't a level, you stupid human.");
                break;
        }



        if (level == 1)
        {
            Terminal.WriteLine("you have selected the preliminary test");
            Terminal.WriteLine("Please enter your selection");
            Terminal.WriteLine("Hint: " + password.Anagram());
        }
        else if (level == 2)
        {
            Terminal.WriteLine("you have selected the intermediate test");
            Terminal.WriteLine("Be advised that answers can now be multiple words");
            Terminal.WriteLine("Please enter your selection");
            Terminal.WriteLine("Hint: " + password.Anagram());
        }
        else if (level == 3)
        {
            Terminal.WriteLine("you have selected the Advanced test");
            Terminal.WriteLine("Be advised that answers can still be multiple words");
            Terminal.WriteLine("Please enter your selection");
            Terminal.WriteLine("Hint: " + password.Anagram());
        }
    }

    void DisplayWinScreen()
    {
        currentScreen = Screen.Win;
        Terminal.ClearScreen();
        ShowLevelReward();
    }

    void ShowLevelReward()
    {
        switch (level)
        {
            case 1:
                Terminal.WriteLine("Have 100 grams of vegetable matter");
                Terminal.WriteLine("The password to access the intermediate level is:"); //implement a password!!
                Terminal.WriteLine("intermediatepass");
                Terminal.WriteLine("You may want to write that down");
                Terminal.WriteLine(" ");
                Terminal.WriteLine("Type Menu to return to the main menu.");
                break;
            case 2:
                Terminal.WriteLine("Great job for making it this far.");
                Terminal.WriteLine("Have 50 grams of protein.");
                Terminal.WriteLine("The password to access the advanced level is:");  //implement a password!!
                Terminal.WriteLine("i_luv4n4gr4ms");
                Terminal.WriteLine("You may want to write that down");
                Terminal.WriteLine(" ");
                Terminal.WriteLine("Type Menu to return to the main menu.");
                break;
            case 3:
                Terminal.WriteLine("WELL DONE!");
                Terminal.WriteLine("You have completed all three ");
                Terminal.WriteLine("challenges.");
                Terminal.WriteLine("You now have access to medicine ");
                Terminal.WriteLine("for a week.");
                Terminal.WriteLine("We are looking forward to testing ");
                Terminal.WriteLine("you again.");
                Terminal.WriteLine(" ");
                Terminal.WriteLine("Type Menu to return to the main menu.");
                break;
            default:
                Debug.LogError("Invalid Level Reached");
                break;
        }

    }
}

Thank you for your help Nina!!

This is the most modern version of the script btw, with different errors than the one above. now I’m getting these errors:
NullReferenceException: Object reference not set to an instance of an object
Terminal.GetDisplayBuffer (Int32 width, Int32 height) (at Assets/WM2000/Terminal/Terminal.cs:21)
Display.Update () (at Assets/WM2000/Display/Display.cs:33)

and

NullReferenceException: Object reference not set to an instance of an object
Terminal.ReceiveFrameInput (System.String input) (at Assets/WM2000/Terminal/Terminal.cs:26)
Keyboard.Update () (at Assets/WM2000/Keyboard/Keyboard.cs:37)

and

Assertion failed: Callback unregistration failed. Callback not registered.

With 200,000+ students to support I’m afraid I do not have time to download anything unless I explicitely asked for it.

NullReferenceException means that a reference (“link”) to an instance is missing. Since your error messages are referring to the Display and the Keyboard class, I assume a string variable was null. Double click on the error messages and post the highlighted lines here.

And try the following:

void OnUserInput(string input)
{
    Debug.Log("input: " + input);

    // rest of your code
}

When you play your game, what does your Console say?

Hey, I actually edited the error messages into the last comment I made!

I’ll try doing that, just to be straight that means that it’s saying the string variable doesn’t have an actual string in it, it’s just an empty variable? So I need to make sure that at the point where the system is checking whether they entered the correct “Levelkey”, the variable “string levelkey” has been set already.

That’s my guess I’ll play around with it. I’ll put the debug into my code and see what happens.
The game doesn’t let me play the game, shows me the compiler errors (in the message above,)

Hey yeah that totally makes sense about the downloading, sorry if it came off rude.

Thanks again for your help!!

Heyy that code you gave me helped a bunch! Now it’s registering levelkey, and it’s letting me enter the levelkey to get to the next level.

Now It’s just thinking that once you enter levelkey you’ve finished that level, and shows the win screen. I need to make it so that it connects to the AskForPassword page afterwards.

Thank you thank you thank you!!

If you want to do something with a string in your code, you need to make sure that you receive a string. A NullReferenceExpection is a very common error every programmer gets from time to time. When you know that it refers to an “empty” variable, it’s not that difficult to fix it.

You have to find the place where the object is supposed to get assigned to the variable, and you have to figure out the reason why it does not get assigned.

1 Like

Ah okay, well thank you so much!! This is a great community and the teachers and teaching assistants are still active on older tutorials which is so wonderful for people like me that needs a lot of nudgin n help.

Hope you have a good day!

As promised:
virtual hugs

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms