• 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 Jun 20, 2014 at 07:46 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by mightybob · Jun 20, 2014 at 03:02 AM · collisiondetection

OnCollisionEnter not working

As usual, OnCollisionEnter stops working at the tiniest little piece of dust that floats by. Well, here's my code, very simple and SHOULD work:

 function OnCollisionEnter(collider:Collision) 
 {
 
 if (collider.gameObject.tag == "ground")
 {
 
 Debug.Log("colliding with ground");
 
 }
     
     if (collider.gameObject.tag == "layerPad")
     {
         Debug.Log("colliding with layer pad");
     }
 }

Alright, layerPad has a rigidbody attached to it plus a box collider. The player has also has a box collider attached to it (no rigidbody (and no, I am not using any transform stuff)). Now when these 2 objects collide, they stop moving since they hit, but no debug.log. They have rigidbodies, they've got box colliders (both of the same type, they are both 3D colliders) the rigidbody is not a 2D one, I'm not using transforms, why the heck doesn't it debug.log? Should't this be working? (they are both marked as not to be a trigger). I've been stuck on this for THREE HOURS. It's driving me nuts. Anyone have any ideas what I am doing wrong?

Comment

People who like this

0 Show 8
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 Jeff-Kesselman · Jun 20, 2014 at 03:03 AM 1
Share

All you have proved is that you are not getting into the body of your ifs.

You are assuming that its because you aren't getting the callback but you have no evidence yet to support that assumption.

Assumptions are deadly and the antithesis of debugging.

The first thing you should do is put a debug at the top of your callback and BEFORE the ifs to see if it gets called.

Then come back and tell us the result and we'll elp you froim there.

avatar image Hikaros · Jun 20, 2014 at 10:04 AM 1
Share

are the objects children of another object? if yes, is the script in the children itself? if no you should access the children.

On another subject, have you tried only "collider.tag" instead of "collider.gameObject.tag"?

avatar image HarshadK · Jun 20, 2014 at 10:12 AM 0
Share

Just to be sure, have you attached this script to your Player game object?

avatar image meat5000 ♦ · Jun 20, 2014 at 10:22 AM 0
Share

The objects are physically colliding, you said.

This leaves the problem to be that the tags are incorrect on the objects that contain the scripts and rigidbodies.

avatar image mightybob · Jun 20, 2014 at 07:04 PM 0
Share

Yep, I have attached the scripts to the desired game objects. And yes, both of the colliders are children of other game objects. Could this be the problem? Also, I've already triple checked tags but I'll try the Debug.Log outside of the if statement as you said. @Jeff sorry, I'll remember that for next time.

Show more comments

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by mightybob · Jun 20, 2014 at 07:45 PM

It's fixed now. For some reason Unity wouldn't collide with children objects. Big thanks to @Hikaros for pointing it out! Here's my finished code for anyone else with the same problem:

 function OnCollisionEnter(collision:Collision)
 {    
     if (collision.gameObject.rigidbody.position.x > rigidbody.position.x) 
     {
         Debug.Log("right collide");
     } 
     else 
     {
         Debug.Log("left collide");
     }
 
     if (collision.gameObject.rigidbody.position.y > rigidbody.position.y) 
     {
         Debug.Log("top collide");
     } 
     else 
     {
         Debug.Log("bottom collide");
         if (collision.gameObject.tag == "layerPad")
         {
             ChangeTo(collision.gameObject.GetComponent(BounceScript).targetLayer, collision.gameObject.GetComponent(BounceScript).targetHeight);
         }
     }
 }
   
Comment

People who like this

0 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 Hikaros · Jun 20, 2014 at 09:21 PM 0
Share

You are welcome xD

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Unity Collison Problem 1 Answer

C# Collision Detection Help 0 Answers

A node in a childnode? 1 Answer

Collision problem when spawning in the collider 1 Answer

Basics on 2d gameplay for a 2.5d Game 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