I am using the above expression Random.Range(0, passwordEasy.Length)
in two different functions but the issue is that it generates different value of passwordEasy array length and that is not what I want. So I thought I should declare a variable randomIndex outside these functions, but then I get the following error :
A field initializer cannot reference
the non-static field, method, or
property ‘Hacker.passwordEasy’
[Assembly-CSharp]
In the code below you will notice I have used Random.Range(0, passwordEasy.Length) twice in two different functions. I don’t want to do that. Here’s the part of my code I am having issue with :
void CheckPassword(string input)
{
if (isEasy == true)
{
if (input == passwordEasy[Random.Range(0, passwordEasy.Length)])
{
DisplayWinScreen();
}
else
{
Terminal.WriteLine("Incorrect Password!");
}
}
else if (isEasy == false)
{
if (input == passwordTough[Random.Range(0, passwordTough.Length)])
{
DisplayWinScreen();
}
else
{
Terminal.WriteLine("Incorrect Password!");
}
}
void DisplayWinScreen()
{
Terminal.WriteLine("Hey ! Correct Password!");
currentScreen = Screen.Win;
ShowLevelReward();
}
void ShowLevelReward()
{
if (isEasy == true)
{
Terminal.WriteLine("EASY WON");
}
else if (isEasy == false)
{
Terminal.WriteLine("Tough WON");
}
}
}
void StartGame(int level)
{
Terminal.ClearScreen();
WriteOnScreen();
}
void WriteOnScreen(){
if (isEasy == false)
{
Terminal.WriteLine("Enter your password, hint : " + passwordTough[Random.Range(0, passwordTough.Length)].Anagram());
}
else if(isEasy == true)
{
Terminal.WriteLine("Enter your password, hint : " + passwordEasy[Random.Range(0, passwordEasy.Length)].Anagram());
}
}