[Solved] Trying to disable keys after they've served their use

I’ve given options to the user for a few different selectable ranges to choose from and to pick one they press the corresponding key (i.e press A for 1-100, b for 1-1000, etc.). It is working well, but after selecting a range and starting with the guesses by pressing the up and down arrows, the keys to select the ranges can still be pressed and reset the range. Is there a way to disable the keys for the selectable ranges after one is pressed and they don’t become enabled again until the game is started over? I’ve been searching online but haven’t found anything that works.

Here’s my code if it helps:

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.

1 Like

Thank you, that was exactly what I was trying to do. I do have one more question though, what’s the difference between = and ==? I’m just not real clear on that.

1 Like

= is an assignment operator (x = 5, the value to 5 is assigned to the x variable)
== is a comparison operator (5==6 is false, 5==5 is true)

int x = 5 (assignment)
x==5 (comparison is true)
x = 6 (assignment)
x==5 (comparison is false now)

2 Likes

Ah, thank you, that clears it up for me. :+1:

1 Like

Privacy & Terms