Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MC_Shadows · Oct 23, 2014 at 12:36 PM · triggersystemsystems

Door and Key System please help

Hello everybody, I'm new to Unity and Scripting in general. I need a Door&Key System, But I can't figure out how to write it. I want the player to find a key, then go in a location (room) and load next level (such as a key for a teleporter or something like this)

Obviously without the key the player can't access the next scene. Can you please help me? Thanks in advance and sorry for my bad english.

Comment
Add comment · Show 4
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 ExtremePowers · Oct 23, 2014 at 12:51 PM 1
Share

You could try attaching this to the player:

 var Has$$anonymous$$ey : boolean;
  
 function OnCollisionEnter(collision : Collision) {
     if (collision.gameObject.name == "Door") { //Or gameObject.tag == "Door" it would be more safe way of doing it if you have a door which doesn't do anything
         if (Has$$anonymous$$ey) {
             Application.LoadLevel(1); //Load level 2
         }
     }
 }

avatar image MC_Shadows · Oct 23, 2014 at 12:57 PM 0
Share

This is good but I also need a key script and an inventory script, qhit this script I tell the door "If the player with the key collides with you, load the next level" But I need a "Has$$anonymous$$ey script" and an Inventory script. Also a Pickup$$anonymous$$ey Script..... :D

avatar image MrSoad · Oct 23, 2014 at 01:06 PM 0
Share

You need to do some reading and learning, you might as well ask us to write your game for you. Inventory scripts can be lengthy...

avatar image MC_Shadows · Oct 23, 2014 at 01:27 PM 0
Share

I don't want you to write my game, I probably asked it in a bad way :D I just need an Idea to figure out how to write a full-working script. :D Sorry for all these smiles :D Can't stop :D O$$anonymous$$G D:

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dimling · Oct 23, 2014 at 01:08 PM

I wont write a lot of scripts for you but i can give you an idea of how this could be done.

The inventory could be a list of gameobjects.

  List<GameObject> listOfGameObjects= new List<GameObject>();
 

When you find a key, for instance OnCollision with key, you can add this gameobject to the list, and maybe delete it from the scene.

 void OnCollisionEnter(Collision objectYouHit){
 
     if (objectYouHit.gameObject.name == "Key") { 
        listOfGameObjects.add(objectYouHit);
        Destroy(objectYouHit);
     }
 }

When you come to the door, you can check the list if there is a key object there, and then use the script that extreme powers presented above to open the door!

Since you are new to Unity and scripting I would advice you to have a look at some tutorials. I would suggest the following youtube videos made by the tornardo twins. Its an easy introduction to unity and a good way to learn some simple scripting!

Heres the youtube link

Comment
Add comment · Show 3 · 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 richyrich · Oct 23, 2014 at 01:19 PM 0
Share

Unity has many of its own tutorials too... http://unity3d.com/learn/tutorials/modules

avatar image Dimling · Oct 23, 2014 at 01:21 PM 0
Share

True! I've just submitted the one i had the best experience with! :)

avatar image richyrich · Oct 23, 2014 at 01:32 PM 0
Share

I meant to put the comment in the OP section :)

avatar image
0

Answer by richyrich · Oct 23, 2014 at 01:06 PM

1) Visit this location for more info on getting the key and door to work

http://answers.unity3d.com/questions/244159/opening-a-door-with-a-key.html

2) Making sure there is a collider on the door and is set up as a trigger, place the following code inside the OnTriggerEnter function... Application.LoadLevel(nextLevel);

Obiviously you need set nextLevel = 2; or something somewere higher up in your program

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

Answer by Sarthak123 · Oct 23, 2014 at 01:55 PM

you can use a very simple trick, that is GameObject.SendMessage and OnTriggerEnter() so first tag your player with the tag "Player" to begin, create a key and a door, make them close just for testing this script quicly and fast.. you can write a script like this for your key,(assuming your key is collectible object like a rotating object in floor) you have to attach an collider which is set to trigger to both the door and your key for these scripts to work.

 var door : GameObject; // door in the scene
 
 function OnTriggerEnter(other : Collider) 
 {
 if(other.tag == "Player") // to check if player has entered or not.
 {
 door.SendMessage("Open"); // sending message to door to open
 Destroy(gameObject); // destroying key since it has been collected.
 }

}

next you need to attach a script to your door which initiates the message "Open" sent by the key...

 var canOpen : boolean = false;
 
 function Open() // will be initiated on recieving message by the key.
 {
 canOpen = true; // tell the door that the key has been collected and now it can be opened if only player reaches to the door, we will check whether the player has reached near the door or not in the next step.
 }
 
 function OnTriggerEnter(other : Collider)
 {
   if(other.tag == "Player" && canOpen) // player has entered and door can be opened or not?
 {
  // do something
 // here write your script how you would open your door, ex. by playing an animation, etc.
 }
 }

this method will definitely work and you can check script referrence for GameObject.SendMessage and OnTriggerEnter for better understanding..!

hope it helps you in your game.

Comment
Add comment · Show 4 · 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 MC_Shadows · Oct 23, 2014 at 02:15 PM 0
Share

Thanks this is useful. Now, i the door script at the end, where you wrote the do something comment etc. Can I just simply write Application.LoadLevel(2); ? (loads the second level in the build settings. will that work?

avatar image MC_Shadows · Oct 23, 2014 at 02:31 PM 0
Share

Thanks $$anonymous$$an :D It worked, I just changed something in the door script, If you try to open without the key a tetx displays and a sound plays, It works perfectly, THAN$$anonymous$$S $$anonymous$$AN :D

avatar image Sarthak123 · Oct 24, 2014 at 08:30 AM 1
Share

no problem, please accept my answer, i will be glad to see that..:)

avatar image Sarthak123 · Oct 25, 2014 at 12:47 PM 0
Share

@$$anonymous$$C_Shadows i meant mark my answer as correct, u just voted my comment...

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Need help making an audio trigger. 2 Answers

How can I teleport objects. 4 Answers

Glitchy Turret Script 2 Answers

Problem with OnTriggerEnter() 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