Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 GameDevDude · Jun 23, 2017 at 12:57 AM · unity 5prefabscene

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vicarian · Jun 23, 2017 at 01:27 AM

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: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines

Comment
Add comment · Show 1 · Share
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 GameDevDude · Jun 23, 2017 at 01:36 AM 0
Share

Thanks that helped a lot, I managed to achieve what i wanted

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.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I add visual effects on the OVRPlayerController? 0 Answers

Passing array of prefabs to next scene 1 Answer

Git and .scene/.prefab 2 Answers

Storing scene-specific variables? 1 Answer

Invalid Serialized Header Error after Project Move 0 Answers


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