Unity Hangs when I enter the password

//After I enter the password it doesn’t go into the ChecPassword function and Unity (and my whole system) suddenly hangs. Can’t figure out why.

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

public class Lets_Hack : MonoBehaviour
{
//Game State
int level;
string password;
string lvl1password = { “dog”, “lion”,“cat”,“tiger”,“bhundru” };
string lvl2password = { “tea”, “coffee”,“shikanji”, “lemon juice”, “icecream”};
string lvl3password = { “vishal parikh”, “kavita tewani”, “vijay ukani”, “monika sharma”, “priyank thakkar” };
enum Screen { MainMenu, Password, Win };
Screen currentScreen;

// Start is called before the first frame update
void Start()
{
    ShowMainMenu("HELLO Khodidas");
}

void ShowMainMenu(string greeting)
{
    currentScreen = Screen.MainMenu;
    Terminal.ClearScreen();
    Terminal.WriteLine(greeting);
    Terminal.WriteLine("What would you like to hack into?\nhmmmm....?");
    Terminal.WriteLine("Levels are from 1-3");
    /*Terminal.WriteLine("Press 1 to hack Jungle");
    Terminal.WriteLine("Press 2 to hack library");
    Terminal.WriteLine("Press 3 to hack NASA");*/
    Terminal.WriteLine("\nHow?....Guess the correct word to hack");
    Terminal.WriteLine("\nSelect level: ");
}

void StartGame()
{
    currentScreen = Screen.Password;
    Terminal.WriteLine("Choosen Level =" + level);
    Terminal.ClearScreen();
    Terminal.WriteLine("Choose a valid Password:");
    
}

void OnUserInput(string input)
{
    input = input.ToLower();
    if (input == "menu")
    {
        ShowMainMenu("Hello Again KDDDDDDD!!!");
    }
    else if (currentScreen == Screen.MainMenu)
        RunMainMenu(input);
    else if (currentScreen == Screen.Password)
    {
        Terminal.WriteLine("CheckPassword");   **// This statement is not executed when enter password**
        CheckPassword(input);
    }
}
private void RunMainMenu(string input)
{
    if (input == "007")
    {
        Terminal.WriteLine("Hello Mr. Bond1!!");
    }
    else if (input == "1" || input == "2" || input == "3")
    {
        level = int.Parse(input);
        StartGame();
    }
    else
        Terminal.WriteLine("Choose a valid level");
}

void CheckPassword(string input)        
{
    Terminal.WriteLine("Inside CheckPAssword");
    int flag = 0;
    int k = RandomPassword();
    Console.WriteLine("K=" + k);
    input = input.ToLower();
    Console.WriteLine(input);

    while (flag != 1)
        switch (level)
        {
            case 1:
                password = lvl1password[k];
                Terminal.WriteLine("Think of an Animal\n");
                if (password == input)
                {
                    flag = 1;
                    Terminal.WriteLine("Correct !!!");
                }
                else
                    Terminal.WriteLine("Awww..... It's not correct but you are very near\nHint: He is man's best friend or he is King of Jungle");
                break;

            case 2:
                password = lvl2password[k];
                Terminal.WriteLine("Think of a refresher\n");
                if (password == input)
                {
                    flag = 1;
                    Terminal.WriteLine("Correct !!!");
                }
                else
                    Terminal.WriteLine("Awww..... It's not correct but you are very near\nHint: You would go to this place to drink it with your GF or Borhter of the above hint");
                break;
            case 3:
                password = lvl3password[k];
                Terminal.WriteLine("Think of a famous faculty of Nirma\n");
                if (password == input)
                {
                    flag = 1;
                    Terminal.WriteLine("Correct !!!");
                }
                else
                    Terminal.WriteLine("Awww..... It's not correct but you are very near\nHint: The founder of \"OK DOST\" or Chinmaya no maal (as simple as that");
                break;
        }

}

int RandomPassword()
{
    System.Random ran = new System.Random();
    return ran.Next(5);
}

}

Hi Khodidas,

Welcome to our community! :slight_smile:

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


See also:

2 Likes

Don’t know if this is your first time coding, if it is, congratulations, I like how ambicious your code is.

I know Nina’s answer in kinda “meh”, but you’ll learn a lot if you try to solve this on your own. In case you need a hint here’s one:

If Unity hangs is usually because of this https://en.wikipedia.org/wiki/Stack_overflow

1 Like

Thankyou Yee

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

Privacy & Terms