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 Chippers · Jul 10, 2012 at 01:32 PM · doorsendmessagekeyopen

Using If functions to open door with a key.

Hi everyone, I'm having a little trouble with my scripting and can't see why this isnt working.

I'm trying to have a script where you pickup a key and use it to open a door.

This is what i'm currently using. Heres the code on the key

 function OnTriggerEnter () {
 Debug.Log("hit key");
 gameObject.Find("KeyDoor").SendMessage("HitKey");
 }

And heres the door.

 var key : boolean =false;
 
 function OnCollisonEnter (Other : Collision){
 if (key == true){
 Destroy(gameObject);
 }
 }
 
 function HitKey (){
 Debug.Log("Message Received");
 key = true;
 }

Right now when i run into the key the game ends its self. Any help would be greatly appreciated!

Comment
Add comment · Show 3
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 CHPedersen · Jul 10, 2012 at 01:44 PM 0
Share

For starters, the function you're looking for is named OnCollisionEnter, not OnCollisonEnter. Correct spelling is crucial in these cases.

avatar image tomekkie2 · Jul 10, 2012 at 01:49 PM 0
Share

"This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached"

  • Check if you have the colliders and rigidbodies setup properly .

avatar image Mr.Jwolf · Jul 10, 2012 at 02:50 PM 0
Share

You could be unlucky and the doors collider is triggered first, and then the keys collider. You should only need one OnCollisionEnter for this. You could set the:

Destroy(gameObject);
in the Hit$$anonymous$$ey function. This way you could remove the doors OnCollisonEnter.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jul 10, 2012 at 03:59 PM

Few things are going wrong:

You are missing the parameter.

 function OnTriggerEnter(other:Collider)

Is the door a child of the player? Because the command below is looking through the hierarchy of the game object the script is attached to.

 gameObject.Find("KeyDoor").SendMessage("HitKey");

I think you meant: gameObject.Find("KeyDoor").SendMessage("HitKey");

As mentioned in comment:

OnCollisonEnter=>OnCollision (Other : Collision){

The way it is done now is not really making sense, your collision functions needs to check what is going on and what is colliding/

 if(other.gameObject.tag=="Player")

What you have now is that when collecting the key, you send the message that turns key to true and if the door is colliding with anything, you destroy the door. Your door is probably cooliding with a wall or else.

Now a solution:

 function OnTriggerEnter (other:Collider) {
    Debug.Log("hit key");
    if(other.gameObject.tag =="Player")
       GameObject.Find("KeyDoor").SendMessage("HitKey");
 }



And heres the door.

 var key : boolean =false;
 
 function OnCollisonEnter (Other : Collision){
    if(other.gameObject.tag=="Player"){
       if (key == true){
          Destroy(gameObject);
          GameObject.Find("Key").SendMessage("DestroyKey"); //If you also want to destroy the key
       }
    }
 }

 function HitKey (){
    Debug.Log("Message Received");
    key = true;
 }


Your player hits the key, and when he reaches the door, the door checks if the player got the key. If the player has it, then the door destroys. I would guess you want to destroy the key too so add:

 function DestroyKey(){
    Destroy(gameObject);
 }
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

9 People are following this question.

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

Related Questions

Press an in-game switch to open a door - help? 2 Answers

GUI text, door and object collider 2 Answers

Open door with key 1 Answer

Make doors open with different keys? 1 Answer

Opening a door with a key 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