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 Moof · Aug 23, 2014 at 09:58 PM · javascriptcollision

function OnCollosionEnter problems

so I have this script so that when the player touches a key, it changes a variable from 0 to 1 and destroys the key. but for some reason, I can't get unity to recognize the collision between the player and the key.

i made a script

 function OnCollisionEnter (col : Collision)
 {
         Debug.Log("hit!");
 
 }

and what this did was show me everything that the player was colliding with. and it worked on everything except for the object of my key (which is just a sphere)

this is my full code for what i want to happen when the player gets the key

 var locklevel : int = 0;
 var keyGet : AudioClip;
 
 function OnCollisionEnter (col : Collision)
 {
     if(col.gameObject.name == "aKey")
     {
         Debug.Log("hit!");
         locklevel = 1;
         audio.PlayOneShot(keyGet);
         Destroy(col.gameObject);
     }
 
 }
 

this script is attached to the first person controller prefab. why won't it recognize a collision with the key? (the key is also instantiated and not placed from the beggining, if that makes a difference)

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 AngryBurritoCoder · Aug 23, 2014 at 10:09 PM 0
Share

Does the key have a big enough collider ? Does your player have a collider ? if so can they both touch ? also maybe the key collider is set to Trigger ? please answer these to have a clearer idea of whats happening

avatar image Scribe · Aug 24, 2014 at 04:15 PM 0
Share

Try using tags and check for those ins$$anonymous$$d, you will often notice that instantiated prefabs with be called something like "a$$anonymous$$ey (clone)" rather than just "a$$anonymous$$ey", so tag you rkey prefab as "$$anonymous$$ey" and do if(col.gameObject.tag == "$$anonymous$$ey") ins$$anonymous$$d.

avatar image Moof · Aug 25, 2014 at 11:35 PM 0
Share

the key is simply a sphere, with a sphere collider not set to trigger. for some reason adding a rigidbody to the player fixed everything

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fabulous-Design · Aug 24, 2014 at 03:52 PM

make shure you got a (sphere/box)collider on the key.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

JavaScript - Reset Characters Position 2 Answers

Help with OnCollision 2 Answers

Adding sound javascript in unity2d 1 Answer

OnCollisionEnter Problem 1 Answer

Stick an object to the ground while moving? 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