Help! If statement for sequenced buttons.

Im making a game where a player can use the guitar then show buttons of the notes to play. So it will show C D E F G A B whenever the player uses the guitar and if he somehow played a sequence such as C D C D respectively, I want something to happen. Something like
`if(sequence is correct){blabla.SetActive(false;}`
How to make it happen

I would store the notes he plays on a string and the correct sequences on a array of strings. The string should not be longer than the longest “sequence” you want to be recognized. Let’s say the biggest sequence you could want the player to play is 10 notes long:

• Every time the player plays a note add it to the string
• Before you add the note, check if the string.Lenght is >=10, if it is, remove the first note stored.
-Everytime after adding a note, with a for statement, go through the whole sequences array and check for a coincidence.

Here is a working solution a coded for you

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Guitar : MonoBehaviour
{
string[] sequences = { "ABCD", "CDCD", "DCFG" };

string currentSequence = "";
public int maxSequenceLength = 4;

private void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
StoreNote("C");
}
if (Input.GetKeyDown(KeyCode.D))
{
StoreNote("D");
}
if (Input.GetKeyDown(KeyCode.E))
{
StoreNote("E");
}
if (Input.GetKeyDown(KeyCode.F))
{
StoreNote("F");
}
if (Input.GetKeyDown(KeyCode.G))
{
StoreNote("G");
}
if (Input.GetKeyDown(KeyCode.A))
{
StoreNote("A");
}
if (Input.GetKeyDown(KeyCode.B))
{
StoreNote("B");
}
}

void StoreNote(string note)
{
if (currentSequence.Length >= maxSequenceLength)
{
currentSequence = currentSequence.Substring(1, currentSequence.Length - 1);
}
currentSequence += note;

CheckForCorrectSequence();
}

void CheckForCorrectSequence()
{
for (int i = 0; i < sequences.Length && currentSequence.Length > 0; i++)
{
if (currentSequence.Contains(sequences*))*
``````

{
Debug.Log("Sequence correct! You performed " + sequences + " correctly");
//maybe erase the notes that have been already checked and have been a correct sequence
//something like
currentSequence = “”;
}
}
}
}

You can add the correct sequence in an array (maybe represent each letter with a number internally) and then every time user presses one of the keys you want, check if this key value is equal to the current array value and increment the pointer if so.

Something like this:

``````int[] correctSequence;
int currentIndex;

void Start(){
correctSequence = new int[5] {1, 2, 3, 1, 3};
currentIndex = 0;
}

void Update(){
int pressedKeyInt;
if (Input.GetKeyUp(Keycode.C)){
pressedKeyInt = KeyToNumber(Keycode.C);  //a function you will write that turns keys into predefined numbers
}
//etc

if (correctSequence[currentIndex] == pressedKeyInt){
//correct key press
currentIndex++;
if (currentIndex >= correctSequence.Length){
//user pressed the whole sequence correctly
}
}
else
{
//wrong key press

}
}
``````