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 Akazukin · Mar 28, 2014 at 09:34 PM · collision

Error with 2d collision detection

I'm very new to Unity, so I'm guessing this is something easy to fix. Anyways the problem is that I have two 2d objects, both have 2d box colliders and one has a 2d rigidbody, but they do not register when they collide. I am not even getting the debuglog to print. I have the collision code attached to the bullet and the bullet also has the rigidbody. The code is

 var move : int = 2;
 
 
 function OnCollisionEnter2D(thing : Collision) 
 {
     if(thing.gameObject.tag == "destructible"){
     
         Destroy(thing.gameObject);
         Destroy(this.gameObject);
             Debug.Log("hit!");
     }
 }
 
 function Update() {
     
     var y = Time.deltaTime * move;
     
     this.transform.Translate(0, y, 0);
     
 }

Oh and the error message I receive is "Script error: OnCollisionEnter2D This message parameter has to be of type: Collision 2D"

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Li0nSword · Mar 28, 2014 at 10:28 PM

From the error you mentioned you simply have to change the variable type from Collision to Collision2D like so

   function OnCollisionEnter2D(thing : Collision2D) 
     {

Also i'm not 100% sure but I don't think the debug.log will print as you print it AFTER destroying the object itself.

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 Key_Less · Mar 28, 2014 at 09:57 PM

Since you are detecting 2D collisions, your OnCollisionEnter2D method needs a parameter of type Collision2D to report back with the 2D information of the object that has collided.

 function OnCollisionEnter2D(thing : Collision2D)
 {
 }
Comment
Add comment · Show 3 · 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 Akazukin · Mar 28, 2014 at 10:11 PM 0
Share

Thanks! That got it working better, I ran into a new error now though. It's saying NullReferenceException: Object reference not set to an instance of an object.. And neither the bullet nor the enemy are being destroyed

avatar image Key_Less · Mar 29, 2014 at 12:36 AM 0
Share

Could you post your updated code and the error message with it please?

avatar image Akazukin · Mar 29, 2014 at 03:27 AM 0
Share
 var move : int = 5;
 
 
 function OnTriggerEnter2D(thing : Collider2D){
     
     if(thing.tag == "destructible"){
         
         Debug.Log("hit!");
         Destroy(thing.gameObject);
         Destroy(this.gameObject);
     }
 }
 
 function Update() {
     
     var y = Time.deltaTime * move;
     
     this.transform.Translate(0, y, 0);
     
     if(this.transform.position.y > 8){
         Destroy(this.gameObject);
     }
 }

That's the finished code, but I got it working finally. I just had to rearrange a few things and remove the ".gameObject.tag" part in the if statement. Thanks a bunch though.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make 2D sprite character stop jittering when running into walls? 2 Answers

How to run a collision between 2 objects only once 1 Answer

Character Controller Collisions Question 1 Answer

how to make object follow mouse cursor while detecting collision 1 Answer

shader change alpha around collision 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