Like the order from left to right will be X,Y,A ( its randomly generated). The player needs to press the buttons in that particular order, so that the the X,Y,A disappear from screen, kind of a pass lock or password. The essential thing I need is that when the player presses “A” at first it will not affect anything because “A” is last in the order from left to right. Do anyone gets me ??? xD sorry if I was not clear … English is not my first language.
List<int>buttonPressed = null;
int[] GetButtonOrder()
{
// Get random buttons and get their index to pass to the array
// I guess you got that part
return array;
}
IEnumerator CheckButtons()
{
touchOrder = GetButtonOrder();
buttonPressed = new List<int>(touchOrder);
bool done = false;
while(done == false)
{
// Check all four buttons and if one is pressed convert it to integer
int pressed = -1;
if(Input.GetKeyDown(KeyCode.JoystickButton0))
{
pressed = 0;
}
// Same for others with 1,2,3
if(pressed != -1)
{
if(pressed == buttonPressed[0])
{
// Inform right press via GUI
buttonPressed.RemoveAt(0);
}
else{
// wrong answer, inform via GUI, reset the list to original (aka start again from beginning)
buttonPressed = null;
buttonPressed = new List<int>(touchOrder);
}
if(buttonPressed.Count == 0){
//You won
done = true;
}
}
yield return null;
}
}
So we start the coroutine with setting an order of button, this comes form the method, I leave that implementation to you as it is fairly simple.
We pass this array to a list. Then start the checking of the buttons, there may be better way to do but at least this one is simple, pressed is set to -1 so if you do not press anything with i a frame, well nothing happens. If you press a button, it is registered then compared to the first item of the list, if they match then , you remove from the list so the next button becomes the first.
Now if you press the wrong one, it resets the list to the original, this may change to fit what you want to do. Finally, if there is no more item in the list, you emptied it so it means you had all the combinations right, we quit.
When you generate the 3 random buttons the player has to press, just put them in a list in the reverse way you want them pressed. So, for example, if you need the player to press X Y A, you’ll have list[0]=X, list[1]=Y, list[2]=A. Also, keep an “index” int variable, which starts out as 0.
Now, when the player presses a button, if that button is “list[index]”, increment index by 1, otherwise set it to 0. Check index after each increment, when index == list.count, the player has successfully pressed the buttons in order.
I’ve just described the algorithm, feel free to use whatever data structures suit your situation.