Scoring system by pressing a series of particular keys

( I’m sorry for my bad English and lack of ability… :C )

I want to make some scoring system that goes up when users press a series of particular keys.

e.g. they press ‘A’ - ‘S’ - ‘D’ - ‘F’ in order, and then they score.

If they get it wrong in the middle, they should start pressing ‘A’ again.

How do I make it? Thank you for hearing my problem !!

It’s my suggestion:

[SerializeField] KeyCode[] keys; // to make sequence in unity editor
void Start()
    {
        StartSequence(keys);
    }
void StartSequence(KeyCode[] keys)
    {
        StartCoroutine(WaitForInput(keys, 0)); // start reading first key from sequence array
    }

    IEnumerator WaitForInput(KeyCode[] key, int index)
    {
        while (!Input.anyKeyDown) // wait for key pressing
        {
            yield return null;
        }
        if (Input.GetKeyDown(key[index])) // if correct key is pressed
        {
            if (index + 1 >= key.Length) // check if is there any key read, if no - give score
                EndOfSequence(); // random void where you can put score funcionallity
            else
                StartCoroutine(WaitForInput(keys, index + 1)); // go for next key in array sequence
        }
        else // if it's bad key, return to first key of sequence.
        {
            StartCoroutine(WaitForInput(keys, 0));
        }
    }

Here’s my suggestion. Code compiles, runs and will reset if you hit the wrong key.

We do this by creating an array of keys, and an index integer to hold our place in the array. When we hit a correct key, we increment through the array. If we hit a wrong key, we revert back to place 0, the beginning of the array.

In the inspector panel, you can create as many key inputs to run through as you want.

    //Our keys to use
    public KeyCode[] Keys;

    //Integer used to index the array
    int Check;

    // Update is called once per frame
    void Update()
    {
        InputCheck();
    }

    void InputCheck()
    {
        //Making sure we don't go above the number of keys (preventing index out of range issues)
        if (Check <= Keys.Length - 1)
        {
            //If we pressed down the right key, increase
            if (Input.GetKeyDown(Keys[Check]))
            {
                Debug.Log("CORRECT KEY!");

                //Increase check, so we check against the next key
                Check++;

                //If we've hit all keys successfully
                if (Check == Keys.Length)
                {
                    //Score increase!
                    Debug.Log("ADD SCORE!");
                }
            }
            //If we pressed down a key, and the key is not the correct key
            else if (Input.anyKeyDown && !Input.GetKeyDown(Keys[Check]))
            {
                Check = 0;

                Debug.Log("WRONG KEY!");
            }
        }
    }