Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed May 13, 2013 at 03:32 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by yazann · May 13, 2013 at 03:01 PM · c#doorkeylock

Pick up key and unlock a locked door

Hello everyone,

I'm trying to get the Player to pick up a key (at the moment a capsule for testing reasons) and open a door (at the moment a cube for testing reasons) that will warp the Player to the next level.

To pick up the key, the player must walk up to it and press E. The key is then stored in an inventory (currently very small). With the key, the player should be able to move on to the next level, but without it, it shouldn't be possible.

Here's my code for the two scripts I've written.

PlayerInventory.cs (attached to the First Person Controller (Player)):

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 
 public class PlayerInventory : MonoBehaviour {
 
 public string[] Inventory = new string[1];
 
     void Start ()
     {
         Inventory[0] = "";
     }
     
 }

and then PlayerInventoryCheck.cs (attached to the "door"):

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 using UnityEditor;
 
 public class PlayerInventoryCheck : PlayerInventory {
 
     void Update () 
     
     {
     
     if (Input.GetKeyDown(KeyCode.E))
     {
         
         string correctKey = "Captain's Key";
         foreach(string x in Inventory)
     
         {
             if (x.Contains(correctKey))
                 {
                     Application.LoadLevel ("Captain's Quarters");
                 }
             else
                 {
                     Debug.Log("Wrong Key");
                 }
         }
     }    
     }
 }

and finally PickUpItem.cs (attached to the key):

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 using UnityEditor;
 
 public class PickUpItem : PlayerInventory {
 
     void Update ()
     {
     
     if (Input.GetKeyDown(KeyCode.E))
     {
         Debug.Log("You picked up a key");
         Inventory[0] = "Captain's Key";
         Debug.Log("You picked up a key");
     }    
     }
 }    

I get no errors in the console, but whenever I press E (wherever I am in the map), it just debugs in the console as: "Wrong Key". This should only be possible when inside the box-collider of the door (which the 'Is Trigger' is checked, by the way).

Also, it doesn't work to actually pick the key up because of this.

Any idea on what might be wrong here? Been sitting with this all day and I'm getting very dizzy.

Thank you.

Comment
Add comment · Show 2
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 Hoeloe · May 13, 2013 at 03:15 PM 0
Share

Where are you doing the collision detection? I can see no code that tests whether the player is inside the door or not.

avatar image yazann · May 13, 2013 at 03:16 PM 0
Share

There is no door, just a box. I'm not sure about the collision detection...

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Hoeloe · May 13, 2013 at 03:30 PM

I'm aware of that, but let's call the box a door for now. Right. You haven't got any collision detection. That's your problem. Essentially, all of this code in your Update sequence is run constantly, in a loop, not just when you're inside the object it's on. Nowhere in your code have you told it to interact with the player at all, you've just told it what to do when you press the E key. It will do exactly what you've told it to - i.e. run the code when you press the E key. You haven't given it any conditions, such as "if the player is inside the door", which is why it doesn't test for that. I suggest you look at some beginner tutorials, as it's quite obvious you're new at this.

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

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Door opening with a key, my scripts and problem 1 Answer

Door Open With Object Pickup 1 Answer

Help scripting Combination lock/ pressure plates 1 Answer

Distribute terrain in zones 3 Answers

How to make 2 doors with 2 diffrent keys 2 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