The game is stock, but the code doesn`t show any error, help!

Hi, i typed the code exactly like ben did.

void PedirPass()
{
Terminal.ClearScreen();
currentScreen = Screen.Password;
Terminal.WriteLine("Elejiste el nivel " + level);
Terminal.WriteLine("Type Pass, Hint: " + password.Anagram()); // The code run until this line
SetRandomPass();
}

then type pass…blabla is not printed in screen.


if i delete password.Anagram it works fine.
like this:

Please help i am stuck here…can not finish the section :frowning:

Hi Ivo,

Welcome to our forum. :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Hi, yes i did.

it looks the same… and the code doesnt show errors...but still doesnt work…

Is the OnUserInput method spelt correctly in your script? Does the PedirPass method get called?
Are there any error messages in your console when you run your game?

i share you my code:

using UnityEngine;

public class Hacker : MonoBehaviour
{
//Game initial data
const string menuHint = “Type Menu for going back”;
stringlevel1passwords = { “bebe”, “auto” , “rojo” , “azul” };
string level2passwords = { “botella”, “garrafa”, “conejo”, “particula” };
string level3passwords = { “atomo”, “esternoclestomastoide”, “micromolecular”, “astronautico” };
//level state
int level;
enum Screen { Menu, Password, Win };
string password;

Screen currentScreen;

// Start is called before the first frame update
void Start()
{

    ShowMainMenu();

}

void ShowMainMenu()
{
    currentScreen = Screen.Menu;
    Terminal.ClearScreen();
    Terminal.WriteLine("Bienvenido al test de IQ");
    Terminal.WriteLine("1 IQ es de 50 a 100?");
    Terminal.WriteLine("2 IQ es de 100 a 200?");
    Terminal.WriteLine("3 IQ es de mayor a 200?");
    Terminal.WriteLine("Elije tu opcion");

}

void OnUserInput(string input)
{
    if (input == "menu")
    {
        ShowMainMenu();
       
    }

    else if (currentScreen == Screen.Menu)
    {
        RunMainMenu(input);

    }

    else if (currentScreen == Screen.Password)
    {
        CheckPassword(input);

    }
}

void RunMainMenu(string input)
{
  
    bool IsValidLevelNumber = (input == "1" || input == "2" || input == "3");
    if (IsValidLevelNumber)
    {
        level = int.Parse(input);
        PedirPass();
    }
  
    else
    {
        Terminal.WriteLine("Elije un nivel valido");
        Terminal.WriteLine(menuHint);
    }
}
void Update()
{
    int index = Random.Range(0, level1passwords.Length);
    print(index);
}
void PedirPass()
{
    Terminal.ClearScreen();
    currentScreen = Screen.Password;
    Terminal.WriteLine("Elejiste el nivel " + level);
    Terminal.WriteLine("Type Pass, Hint: " ); // THIS LINE MAKE THE ERROR if I set: password.Anagram()
    SetRandomPass();
}

void SetRandomPass()
{
    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("no valid level");
            break;

    }
}

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

    }
    else
    {
        PedirPass();
    }
}

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

void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine(“Bien hecho niño!”);
Terminal.WriteLine(@"
< >
o…o


m “);
break;
case 2:
Terminal.WriteLine(“Bien hecho hombrecito!”);
Terminal.WriteLine(@”
HHHHH
HHH
HHH
HHH
HHHHHHHHHHH “);
break;
case 3:
Terminal.WriteLine(“Bien hecho Einstein!”);
Terminal.WriteLine(@”
%%%%%%%
%% O O %%
%% Y %%
%% _/ %%
_
__/
<>
/
/ ");
break;
default:
Debug.LogError(“no valid answer”);
break;

    }
}

}

Where is Paasword.Anagram defined?

Here!

i notice this error in the console!

NullReferenceException means that a reference (“link”) to an instance is missing.

The best would be to remove the WM2000 script from your Hierarchy. Drag the WM2000 prefab from your Assets folder into the Hierarchy again. Assign your Hacker script to the WM2000 game object in the Hierarchy. Then run your game again.

Did this fix it?

Hi Ivo,

i see you declared a variable string called password. Now you are trying to call a method password.Anagram() but the standard string probably hasnt got such method, it is just for normal strings.

I think you made a typo or got confused a bit and meant Password.Anagram() with a capital P, and not password.Anagram().
To be sure, at the spot where your code goes wrong just type Password and a dot directly after it Visual Studio or Visual Code will show you a list of options. Look if Anagram() is one of them and click on it. That way you also dont have to type so much.

Kind regards,
BR50Kab

This topic was automatically closed after 2 days. New replies are no longer allowed.

Privacy & Terms