Password not appearing in debug panel and print statement running twice

In the above .png file, you can see the message “You have chosen…” written twice and in the right debug panel, you can see the array of passwords appearing but not the password. Also the level is set at 0 rather than 2.

Here is my source code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour {

    //Game configuration data
    string[] level1Passwords = { "books", "aisle", "self", "password", "font", "borrow" };
    string[] level2Passwords = { "prisoner", "handcuffs", "holster", "uniform", "arrest" };


    //Game state
    int level;//member variable available everywhere
    enum Screen {MainMenu, Password, Win };
    Screen currentScreen;
    string password;
    
    // Use this for initialization
    void Start ()
    {
        ShowMainMenu();
    }
	
    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Where do you want to hack into?\n");
        Terminal.WriteLine("1. Local Library");
        Terminal.WriteLine("2. Police Station");
        Terminal.WriteLine("Enter your selection.");
    }

    void OnUserInput(string input)
    {
        if (input == "menu")
        {
            ShowMainMenu();
        }
        else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }

    }
    void RunMainMenu(string input)
    {
        if (input == "1")
        {
            level = 1;
            password = level1Passwords[2]; //todo make random later
            StartGame();
        }
        else if(input == "2")
        {
            level = 2;
            password = level2Passwords[4];
            StartGame();
        }
        else if(input == "007")
        {
            print("Where are the beeyatches, Mr. Bond");
        }
        else
        {
            Terminal.WriteLine("Please choose a valid level");
        }
    }

    void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.WriteLine("You have chosen level " + level);
        Terminal.WriteLine("Please enter a password:");
    }

    void CheckPassword(string input)
    {
        if(input == password)
        {
            Terminal.WriteLine("You got it right!");
        }
        else
        {
            Terminal.WriteLine("Sorry that is wrong!");
        }

    }
}

Hi,

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

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

Privacy & Terms