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 Wurstwolf · Feb 06, 2014 at 08:35 PM · ontriggerenterinventoryjava

Key and Door Tutorial Help?

I was following a tutorial to make a key open a door to lead to the end of the game/the next level, but I can't physically pick up the key to an inventory and therefore I can't open the door. These are the scripts I'm using:

Door Script -

 var Key : GameObject;
 private var Door = false;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.E) && Door == true && Key.active == false)
     {
     Application.LoadLevel("NextLevel");
     }
 }
 
 function OnTriggerEnter (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         Door = true;
     }
 }
 
 function OnTriggerExit (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         Door = false;
     }
 }

Key Script -

 var TheKey : GameObject;
 private var playerNextToKey = false;
 
 function Update () 
 {
      if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
     {
     TheKey.active = false;
     }
 }
 
 function OnTriggerEnter (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         playerNextToKey = true;
     }
 }
 
 function OnTriggerExit (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         playerNextToKey = false;
     }
 }

Please can someone tell me how to physically pick up the key so that I can then unlock the door to either end the game (preferably) or lead to the next level.

HELP NEEDED ASAP, THIS GAME HAS A DEADLINE!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Maui-M · Feb 06, 2014 at 09:03 PM

The script looks fine. I think you need to change the tag on your main character object to "Player".

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 geroppo · Feb 06, 2014 at 10:05 PM

If you just need an easy and fast way to make an inventory of keys, create an inventory script like so

 public class Inventory
 {
     List<Key> keys=new blabla;
     public bool hasKey(Key key)
     {
         // And here just check if the received key( from the 
         // door or something else) is in the inventory.
         return true;
     }
 }

then in the door you can make something like :

 void Update()
 {
      if(Input.GetKeyDown(KeyCode.E)&& Door)
      {

          //get a reference to the player that has the inventory, and then

          if(player.GetComponent<Inventory>().haskey(key))
          {
              //load level
          }
      }
       
 }

this is the fast and easy way, i wouldn't do it this way but since you are in a hurry ,this will do i suppose

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

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

19 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

Related Questions

java script problem 0 Answers

OnTriggerEnter / Inventory question?! 1 Answer

OnTriggerStay and OnTriggerEnter won't react when the player enters them. 1 Answer

Simple Java Script for Opening and Closing a Drawer 1 Answer

[SOLVED] Problem with "foreach". 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