- Home /
How do I force the player to press buttons in order?
Essentially, my problem is this. I'm attempting to create a puzzle in which the player must press three buttons in a particular order. Say there are three buttons, labeled 1, 2 and 3. The sequence in which they must be pressed is 2-3-1. How would I code that? I attempted to create a variable and then add and subtract to it, and if the number was correct that the puzzle would complete, but it's not quite working.
The buttons are simply spheres in the game world, right now with a OnMouseDown function that plays a sound when they are clicked.
As a full disclaimer, I'm a complete newbie to programming (though I know a bit about Unity in general). The most simple and elegant solution would probably be best.
Answer by robertbu · May 03, 2014 at 04:59 AM
Put this script on each of your spheres. Then set the 'digit' to whatever digit that sphere represents. Edit the the 'solution' array in the script to change the solution.
#pragma strict
public var digit = 1;
static var solution : int[] = [3, 2, 1];
private static var input : int[] = [-1, -1, -1];
function OnMouseDown() {
input[0] = input[1];
input[1] = input[2];
input[2] = digit;
if (solution[0] == input[0] && solution[1] == input[1] && solution[2] == input[2]) {
Debug.Log("Correct Answer");
}
}
Thanks for this clever piece of code @robertbu and sorry for digging that up, but I have a complementary question: I would also like to identify when the user has entered the false sequence, but only after he has clicked 3 times. The following condition does not work:
else {
Debug.Log("False Order");
// make something else
}
because each time the users clicks an object, regardless if he is following the correct sequence or not, I get the false message in my console. Any ideas?