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 mdagreda · Mar 15, 2013 at 07:36 PM · collisiononcollisionentercollisiondetection

onCollisionEnter not working

I am trying to use onCollisionEnter to figure out if an object is colliding with another with a tag of pickup. Here is my the code I have that is attached to the object I want to be detecting collision.

 function Update () {
 
 
 
 }
 
  function OnCollisionEnter(con : Collision) {         
    if(con.collider.tag == "pickup"){
        Pickup = true;
    }
    
  }

I'm not getting an error. But the OnCollisionEnter never seems to be going. I have a rigid body attached the the gameobject. I have tried putting a print statment into the function as the first line but I never get anything back. Any ideas what I'm doing wrong?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by murkantilism · Mar 15, 2013 at 07:58 PM

First double check that the object's tag is spelled "pickup", I think this is case-sensitive.

If a typo isn't the error, double check that your script is actually attached to a GameObject inside the scene.

If both of these checks are verified, then I'm not sure. You'd need to post more of your code or where/how it's being used.

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 Josie · Mar 15, 2013 at 09:10 PM

On line 7 change

 function OnCollisionEnter(con : Collision) {

to

 function OnCollisionEnter(other : Collision) {

then change line 8 to

 if(other.tag == "pickup"){

Im not sure if you stated your var but if you didn't then do this

 var Pickup : boolean = false



Hope this helps

Comment
Add comment · Show 1 · 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 murkantilism · Mar 15, 2013 at 09:12 PM 1
Share

Why would changing the name "con" to "other" make a difference?

And declaring the boolean var ahead of time is good practice, but shouldn't be a problem.

avatar image
0

Answer by Polinator · Mar 15, 2013 at 09:27 PM

I think

  if(con.collider.tag == "pickup"){

should be changed to

  if(con.gameObject.tag == "pickup"){

At least that's how my colliders work.

You may want to look at the Script Reference, in case you're missing an attached component to one of the objects. Also make sure you're not mixing up OnCollisionEnter() with OnTriggerEnter(), I have done that a few times myself.

Also, if you still can't get anything to work, use

  function OnCollisionEnter(con : Collision) {         
    print (con.gameObject.tag);
     //if(con.collider.tag == "pickup"){
     //Pickup = true;
    }

To see what you're colliding with. And uncomment if it's working as intended.

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 whydoidoit · Mar 15, 2013 at 09:56 PM

The rigidbody attached to this script must not be asleep (not moved for a while) or must be hit by something that is moving and has a rigidbody and a collider.

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

14 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

Related Questions

Does OnCollisionEnter only get called when the scripted object initiates the collision? 4 Answers

Response speed in OnCollisionEnter / OnTriggerEnter 1 Answer

Testing for collisions prior to placing an object 0 Answers

Collision detection general approaches in Unity3D for C# 1 Answer

Delayed Collisions Bug (includes video demonstration) 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