How can i organize multiple animations? Need help to do a combination

Viewed 48

i have a problem in Unity3d scripts.
I'm trying to make a sort of combination to open a box.
To open this box, i have to insert a combination of 3 buttons correctly.
This 3 buttons(That are simple GameObject, already placed in my scene) have already an animation, when my character collide with one of them, this one will fall down (same animation to other 2).
So, the combination that i want to insert is "the first correct button is "Button n*2", the second correct button is "Button n*1" and the third correct button is "Button n*3"", but i really don't have idea of how i have to do this.
I tried with if statements but if for example the combination is 123-312-123 the animation of the boxes will show up.
I want that only if i do the combination 213 the box is open, than if i go wrong i have to repeat the combination.
Can anyone help me?

1 Answers

Simple way, have a collection of right sequence:

int[] solution = new int[]{2,1,3};

then anytime a button is used, add its value to another collection:

List<int> sequence = new list<int>();
void OnPress(int buttonValue)
{
    if(sequence.Contains(buttonValue)){ return; } // Don't add twice
    sequence.Add(buttonValue);
    if(sequence.Count == solution.Length)
    {
         if(CompareSequence())
         {
               // win
         }
         else
         {
             sequence.Clear();
         }
    }
}

bool CompareSequence()
{
    // this should not be since we checked before but just to be sure
    if(solution.Length != sequence.Count){ return false; }
    for(int i = 0; i < solution.Length; i++)
    {
         if(solution[i] != sequence[i]){ return false; }
    }
    return true;
}

Each action on button would pass its own value that gets added to the list. When the list and the solution are same length, they get compared. If they are same, you move to win section, if not, the sequence is cleared and user needs to refill content.

Related