• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bluelight413 · Mar 12, 2017 at 03:19 PM · prefabsbuttonsdoorslinking

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amonroejj · Jul 05, 2018 at 10:50 PM 0
Share

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)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Scripts/buttons getting old (prefab?) values of variables 1 Answer

How to load instances of a prefab to an array c# 2 Answers

Find Script from another script 2 Answers

Question about instatiating prefab/clone character and how they work/interact with non prefeb buttons 0 Answers

Question about instatiating prefab/clone character and how they work/interact with non prefeb buttons 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges