Any chance you could show me the Hacker code - I have been doing the 2D course of this but I can try and help if no one else can.
Hi @diganth_kavitha,
Welcome to our community!
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
I have tried every possible way but the result was the same
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hacker : MonoBehaviour
{
string[] level1Passwords = { "book", "under", "hero", "elves", "dragon", "villain" };
string[] level2Passwords = { "Dragonite", "command", "Blackbox", "Superman", "Nuclear" };
string[] level3Passwords = { "accumination", "satisfation", "transportation", "quicksilver", "dehydration" };
int level;
enum Screen {MainMenu, Password, Win };
Screen cureentScreen;
string password;
// Start is called before the first frame update
void Start()
{
ShowMainMenu();
}
void ShowMainMenu()
{
cureentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine("Terminal Hacker");
Terminal.WriteLine("Hey GUYS ,Lets play this game");
Terminal.WriteLine("Select:");
Terminal.WriteLine("1 for Easy");
Terminal.WriteLine("2 for Medium");
Terminal.WriteLine("3 for Hard");
}
void OnUserInput(string input)
{
if (input == "menu")
{
ShowMainMenu();
}
if (cureentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
if (cureentScreen == Screen.Password)
{
CheckPassword(input);
}
}
void RunMainMenu(string input)
{
bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
if (isValidLevelNumber)
{
level = int.Parse(input);
StartGame();
}
else if (input == "007")
{
Terminal.WriteLine("I will call you Mr James Bond");
}
else if (input == "Batman")
{
Terminal.WriteLine("so you are batman");
}
else
{
Terminal.WriteLine("please select a valid level");
}
}
void StartGame()
{
cureentScreen = Screen.Password;
Terminal.ClearScreen();
Terminal.WriteLine("Please enter the password :");
switch (level)
{
case 1:
password = level1Passwords[0];
break;
case 2:
password = level2Passwords[0];
break;
default:
Debug.LogError("Invalid error number");
break;
}
}
void CheckPassword(string input)
{
if (input == password)
{
Terminal.WriteLine("super and WELL DONE!");
}
else
{
Terminal.WriteLine("Sorry, wrong password!");
}
}
}
Add the following at the top of your OnUserInput method:
void OnUserInput(string input)
{
Debug.Log("password: " + password);
Debug.Log("input: " + input);
// rest of your code
}
Run your game, type something and check your console. Maybe there is a typo somewhere.
password: AND input:1
I will also send you my code
`using UnityEngine;
public class Hacker : MonoBehaviour
{
string level1Passwords = { “book”, “under”, “hero”, “elves”, “dragon”, “villain” };
string level2Passwords = { “Dragonite”, “command”, “Blackbox”, “Superman”, “Nuclear” };
string level3Passwords = { “accumination”, “satisfation”, “transportation”, “quicksilver”, “dehydration” };
int level;
enum Screen {MainMenu, Password, Win };
Screen cureentScreen;
string password;
// Start is called before the first frame update
void Start()
{
ShowMainMenu();
}
void ShowMainMenu()
{
cureentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine("Terminal Hacker");
Terminal.WriteLine("Hey GUYS ,Lets play this game");
Terminal.WriteLine("Select:");
Terminal.WriteLine("1 for Easy");
Terminal.WriteLine("2 for Medium");
Terminal.WriteLine("3 for Hard");
}
void OnUserInput(string input)
{
Debug.Log("password: " + password);
Debug.Log("input: " + input);
if (input == "menu")
{
ShowMainMenu();
}
if (cureentScreen == Screen.MainMenu)
{
RunMainMenu(input);
}
if (cureentScreen == Screen.Password)
{
CheckPassword(input);
}
}
void RunMainMenu(string input)
{
bool isValidLevelNumber = (input == "1" || input == "2" || input == "3");
if (isValidLevelNumber)
{
level = int.Parse(input);
StartGame();
}
else if (input == "007")
{
Terminal.WriteLine("I will call you Mr James Bond");
}
else if (input == "Batman")
{
Terminal.WriteLine("so you are batman");
}
else
{
Terminal.WriteLine("please select a valid level");
}
}
void StartGame()
{
cureentScreen = Screen.Password;
Terminal.ClearScreen();
Terminal.WriteLine("Please enter the password :");
switch (level)
{
case 1:
int index = Random.Range(0, level1Passwords.Length);
password = level1Passwords[index];
break;
case 2:
int index2 = Random.Range(0, level2Passwords.Length);
password = level2Passwords[index2];
break;
case 3:
int index3 = Random.Range(0, level3Passwords.Length);
password = level3Passwords[index3];
break;
default:
Debug.LogError("Invalid error number");
break;
}
}
void CheckPassword(string input)
{
if (input == password)
{
DisplayWinScreen();
}
else
{
Terminal.WriteLine("Sorry, wrong password!");
}
}
void DisplayWinScreen()
{
cureentScreen = Screen.Win;
Terminal.ClearScreen();
ShowLevelReward();
}
void ShowLevelReward()
{
switch (level)
{
case 1:
Terminal.WriteLine(@"
____________________
/ //
/ //
/ //
/ //
/ //
/ //
/ //
///
((/
you got a book… “);
break;
case 2:
Terminal.WriteLine(@”
_____
/ _______________
/ |
\ ___==|
_/
YOU GOT A PRISON KEY");
break;
case 3:
Terminal.WriteLine(@"
_________
______ / BE \ _______
|| IMMORTAL||
_________/
");
break;
}
}
}
`
The message is fine so far. At first, you have to select a level, e.g. 1. Then you should be able to type your password.
By the way, the second and third if
in OnUserInput must be else if
. Remember you can also look at the lecture code changes via the link in the Resources of each lecture.
See also:
- Forum User Guides : How to apply code formatting within your post
- Forum User Guides : How to mark a topic as solved
thanks
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.