Hello @Ly0n0,
The issue is that you are still checking for these key presses in the Update()
method.
As NumberWizard doesn’t have a start scene, or an options scene, one way to circumvent this issue would be to set a flag once these options have been chosen.
Example;
public class NumberWizard2 : MonoBehaviour
{
int max;
int min;
int guess;
bool optionRangeSelected;
// Use this for initialization
void Start()
{
optionRangeSelected = false;
print("Welcome to Number Wizard!");
print("I will guess your number, but first I need a range.");
print("Press A for 1-100, B for 1-1000, or C for 1-100,000.");
}
void InitialGuess()
{
guess = Random.Range(min, max);
print("Is it " + guess);
max = max + 1;
}
// Update is called once per frame
void Update()
{
if (optionRangeSelected == false)
{
if (Input.GetKeyDown(KeyCode.A))
{
optionRangeSelected = true;
max = 100;
min = 1;
print("Alright. 1-100");
InitialGuess();
}
else if (Input.GetKeyDown(KeyCode.B))
{
optionRangeSelected = true;
max = 1000;
min = 1;
print("Okay. 1-1000");
InitialGuess();
}
else if (Input.GetKeyDown(KeyCode.C))
{
optionRangeSelected = true;
max = 100000;
min = 1;
print("Making it hard, eh?");
InitialGuess();
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
min = guess;
guess = Random.Range(min, max);
print("Higher? Is it " + guess);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
max = guess;
guess = Random.Range(min, max);
print("Lower? Is it " + guess);
}
else if (Input.GetKeyDown(KeyCode.Return))
{
print("Got it!");
Start();
}
}
}
In the above, you start off having a flag, optionRangeSelected
set to false in your Start()
method.
Once into the Update()
method, if the flag is false then checking for key presses for your range options is allowed. This will happen until a range is selected.
Once a range is selected, optionRangeSelected
is set to true, in the next Update()
call, the key presses for the range options will now be ignored.
The game plays out until you win and call the Start()
method again where the flag is reset and you select a range again.
Hope this helps.