- Home /
Multiple Doors and Buttons
I've been looking around for answers, and I can't find a solution to this problem.
I'm trying to make a puzzle dungeon game like Zelda, etc. I want to have multiple doors in the scene, as well as multiple buttons. I want to be able to have one door prefab, and one button prefab, and be able to drag these into the scene, and then "hook up" the doors to certain buttons in the scene, so that each door can only be opened after the button it has been hooked up to has been pressed.
So far, I've managed to create a button script that accepts a door object with its script as a public variable. However, stepping on the button causes all doors with the same script to become unlocked.
The way I have it going, I could create a separate script for each button and each door, but then I would have to create another copy of the scripts for each one I add to the scene. Is there a way to make the button only unlock the door that I specify?
Here is my button script:
public bool inTrigger;
public LockedDoor doorTarget;
void OnTriggerEnter(Collider Collider)
{
if (Collider.gameObject.name == "Player")
{
inTrigger = true;
}
}
void OnTriggerExit(Collider Collider)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
doorTarget.Unlocked();
}
}
And this is my door script:
public static bool doorKey;
public bool open;
public bool inTrigger;
void OnTriggerEnter(Collider Collider)
{
if (Collider.gameObject.name == "Player")
{
inTrigger = true;
}
}
void OnTriggerExit(Collider Collider)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (doorKey)
{
if (Input.GetKeyDown(KeyCode.E))
{
open = true;
}
}
}
if (open)
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
else
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
}
void OnGUI()
{
if (inTrigger)
{
if (doorKey)
{
if (!open)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press E to open");
}
}
else
{
GUI.Box(new Rect(0, 0, 200, 25), "Locked");
}
}
}
public void Unlocked()
{
doorKey = true;
}
I don't have an answer per se, but this article may be useful to you: http://infernohawke.blogspot.com/2015/09/how-i-used-observer-pattern-to.html
Per the article: 1.Create an Observer class ; in this case well call it GameTriggers 2. In the script for Triggers have them send the GameTrigger class a message that they have been turned on or off and their internal ID number 3. In the script for the Receiver items subscribe/observe the GameTrigger events so they can be informed when triggers are hit 4. Create a script that performs the actions needed when the appropriate trigger(s) are hit (whether one or ten) for the receiver useage case (door, trap, etc)
Your answer
Follow this Question
Related Questions
Calling a function via button from instance of prefab 1 Answer
Door opener 1 Answer
Help with Scripting (Boolean) 1 Answer