How to make a locked door that unlocks when an event is triggered, for example, a key is picked up.

I’m using the door manager asset, as I am extremely new to coding in unity. I’m trying to make a small indie game alone, and i have the introduction scene done. I want the first objective to be, say drink a cup of coffee to proceed, so what I’m thinking is have the door locked, and it display a message saying you can’t proceed yet (because i don’t want people seeing the next part until interacting with said object, ie coffee is complete, so that i can also use that interaction as a trigger for an explosion. What I’m thinking is that the coffee be like a simple key, which triggers the explosion and allows you to interact with the door. Sorry if the explanation isn’t clear, and also sorry for asking for such in depth help. Ps. Again, I’m using the door manager prefab for my door

You can use events to let the door know to unlock, and to trigger explosions and the like. Unity has a UnityEvent class, though I haven’t used it much. I just tend to use vanilla EventHandlers in the Publisher/Subscriber design pattern.

Let’s make an Event Publisher:

using System;

public class Publisher
{
    public EventHandler RaiseSomeEvent;
    
    public void DoSomething()
    {
        OnRaiseSomeEvent(new EventArgs));
    }

    protected virtual void OnRaiseSomeEvent(EventArgs e)
    {
        EventHandler handler = RaiseSomeEvent;

        if (handler != null)
            handler(this, e);
    }
}

We have something to subscribe to, so need a Subscriber:

using System;
using UnityEngine;
    
public class Subscriber
{
    public Subscriber(string someIdentifier, Publisher pub)
    {
        SomeIdentifier = someIdentifier;
        pub.RaiseSomeEvent += HandleRaiseSomeEvent;
    }

    private void HandleSomeEvent(object sender, EventArgs e)
    {
        Debug.Log("Subscriber " + SomeIdentifier + " did something!");
    }

    public string SomeIdentifier { get; private set; }
}

Put it together:

using UnityEngine;

public class SomeDriverClass : MonoBehaviour
{
    void Start() {
        Publisher  p  = new Publisher();
        Subscriber s1 = new Subscriber("s1", p);
        Subscriber s2 = new Subscriber("s2", p);

        p.DoSomething();
    }
}

// Output:
// Subscriber s1 did something!
// Subscriber s2 did something!

It seems like a bunch of boilerplate code, but it saves a lot of time when you need to make objects interdependent, but don’t want to strongly couple those objects. The only coupling done is on the constructor of the subscriber, so it’s resilient to refactoring code, since all you need to do is change the class it subscribes to, rather than the EventHandler the Subscriber wants to subscribe to. Handling the code is also done (in general) with the Subscriber, though if you need to prevent race conditions, then you might drop some code into the OnRaise… events. They’re virtual as well, so if you need to derive the Publisher to change how it behaves, that’s a snap. Pretty cool to have one method call cause 2 outputs, huh?

Source for code sample: Publish events that conform to .NET Guidelines - C# Programming Guide | Microsoft Learn