Run into an Error

Hello! I’m still very new to this whole C# / Unity thing. Taking the fantastic 3D Developement course on udemy made by Ben, but have run into a bit of a problem. After finishing up the hacker game from section 1 (simple game where the player unscrambles anagrams to ‘hack’ into different companies and win) we were challenged to develop the game further and introduce our own concepts. With three different difficulty levels available, each level throws you a longer and harder anagram to unscramble, providing the difficulty. I have been trying to make it to where the player must unscramble two anagrams for level 2, and three for level 3 etc. I began by setting the different passwords in my switch statement:

I then modified my ‘CheckPassword’ function:

However I have now run into an error. Upon entering the scrambled password, ‘DisplayWinScreen’ is no longer executed. I am then forced to return to the menu. Could this be an error in how I blocked together my code? Any help with this would be greatly appreciated.

Hi Sam,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

1 Like

Thank you for the help! Do you think I should repost this with the entire script? Or if I just paste the sections in the photo that’d be enough? Sorry not too sure how this all works yet

Please post the relevant code only. Alternatively, post the entire script on pastebin.com and share a link here.

What error do you get? A NullReferenceException or something like that? Or is the error that something does not get executed? Have you already added Debug.Logs to your code to see which code blocks get executed?

The error is that, after modification, a certain function will not execute and it’s not apparent to me why. Here’s the whole script: https://pastebin.com/mJDAxtQR

    void CheckPassword(string input)
    {
        if (input == "1")
        {
            if (input == password)
            {
                DisplayWinScreen();
            }
            else
            {
                AskForPassword();
            }
        }
        else if (input == "2")
        {
            if (input == password)
            {
                Terminal.WriteLine("Now for the second password. Hint: " + password2.Anagram());
                if (input == password2)
                {
                    DisplayWinScreen();
                }
                else
                {
                    AskForPassword();
                }
            }
        }
        else if (input == "3")
        {
            if (input == password)
            {
                Terminal.WriteLine("Now for the second password. Hint: " + password2.Anagram());
                if (input == password2)
                {
                    Terminal.WriteLine("Now for the third password. Hint: " + password3.Anagram());
                    if (input == password3)
                    {
                        DisplayWinScreen();
                    }
                    else
                    {
                        AskForPassword();
                    }
                }
            }
        }
    }

Above is the code that I modified, along with under the switch statement to add password 2 & 3. For some reason now DisplayWinScreen() will not execute here. And yes, it appears everything works up until just that. Thanks again for your time looking over this!

Thank you.

I think the issue is the following:

if (input == "1")
{
    if (input == password)
    {
        DisplayWinScreen();
    }

    // code
}

Assuming the player typed 1, the “1” string gets assigned to input. The first if-condition gets evaluated to true. Now the inner if-condition gets evaluated. input is still “1”. If password is not “1”, the condition is false, and the code block does not get executed. This is just one example from your code. There are more nested ifs that compare something to input.

I’m not entirely sure what your logic is but bear in mind that the computer cannot remember anything, and it does not wait for user input. When CheckPassword gets called, the whole code block gets executed.

Depending on what your idea is, you probably need a second variable to keep track of the level (?).

Hi,

As this has been 22 days since the last reply i am closing this topic in 7 days.
If you need help after this time please either contact a moderator for this to be unlocked or start a new topic.

Thanks in advance

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