Combination lock

What would be a good way to check in what order booleans from different scripts are activated?

I’m trying to create a combination lock, and I have two scripts; One goes on the locked object (door, gate etc) and the other script goes on the triggers (levers, button etc). The trigger scripts have a boolean each called activated.

In the LockObject script, I want to check in what order the “activated” booleans are enabled. Is there any way to do that?

One way that you can do this is by creating a static int member variable on your script (you can hide it from the editor). This will be a value that is incremented each time an item is triggered.

Also, create a non-static int field in order to hold the order value. The relevant part of your class would look something like this:

using UnityEngine;

public class LockObject : MonoBehaviour {

    private static int counter = 0;

    public bool triggered;
    public int triggerOrder;

    public static int GetCounter()
    {
        return ++counter;
    }

    void SetTrigger ()
    {
        triggered = true;
        triggerOrder = GetCounter();
	}
}

If you use the SetTrigger() call in order to set the item as triggered, then the triggerOrder will be incremented and assigned to the triggerOrder property. Each subsequent trigger (regardless of the game object on which the script is attached) will be the next highest number.

Because the counter itself is static, it is shared by all scripts, at the time an item is triggered, the scripts own copy of the order value will be set, so it should be unique to each script with each call of SetTrigger().

If you get the collection of LockObject scripts attached to your game objects, you can sort them by the triggerOrder property and you’ll know the specific order in which the items were triggered.