1
\$\begingroup\$

This is my first ever question here; I'm pretty stuck on a certain feature I'm working on in order to learn.

I'm working in Unity 3D. I'm trying to make a set of buttons that work in unison to unlock a door. The general idea is that there's 5 buttons, and the player has to walk on them in a specific order, for the door to open.

Currently, I have the individual buttons working - if the player walks on one, it turns it on and changes the colour, if he walks on the button again, it turns it off and reverts back to the initial colour, so my individual buttons are fine and work both ways (on/off).

Where I am stuck, is creating a relationship between the buttons. So for example, if button 1 is on and the player walks onto button 5, I want button 1 to reset to the off state but I'm totally lost on how to develop a relationship between these individual buttons!

So far, I've theorized that I need an empty game object with a script attached that controls the relationship, and change in state dependent on the order, and I'm guessing that it needs to involve a switch case statement, but beyond that I'm both totally unsure!

\$\endgroup\$
2
  • \$\begingroup\$ Do you want a previously stepped on button to always reset when a new button is used or do you want them to only reset if the user gets them out of order? \$\endgroup\$
    – Pikalek
    Dec 10, 2021 at 15:50
  • \$\begingroup\$ only when out of order- so if the player gets the right order the buttons stay on and when all are on, the door opens. if they get the wrong order, the buttons turn off! \$\endgroup\$ Dec 10, 2021 at 15:52

1 Answer 1

2
\$\begingroup\$

A generic way to solve this is with two lists.

One list holds the correct answer to the puzzle. It could be initialized from a file, generated procedurally or hard coded - doesn't really matter. The important part is, you have data with the right answer.

The second list holds the player's attempt. Initially it will be empty. As the player steps through the correct sequence of buttons, they will get added to the list. When the player makes a mistake it will get reset.

To process things, every time the player walks off a button, do the following:

  • add that button's ID to the attempt list
  • loop through the attempt list & compare each entry to the answer
    • if while looping, there's an entry that doesn't match
      • clear the attempt
      • reset the button colors
  • if no mismatches were detected & the attempt is the same size as the answer, open the door

The processing code might look something like this:

attempt.Add(id);
for(int a=0; a<attempt.Count; a++)
{
   if(attempt[a] != answer[a])
   {
      attempt.Clear();
      ResetAllButtons();
   }
}
if(attempt.Count == answer.Count)
{
  OpenDoor();
}
\$\endgroup\$
2
  • \$\begingroup\$ thanks so much, cant upvote you but i am in spirit! \$\endgroup\$ Dec 10, 2021 at 17:55
  • \$\begingroup\$ @russiantortoise If you believe this answer is the best solution to your problem, you can mark it as accepted. Good luck with your project! \$\endgroup\$
    – Pikalek
    Dec 10, 2021 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .