Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Phosphorus2500 · Feb 08, 2016 at 12:37 PM · 2dcollisionrigidbodycolliderdestroy

Why Isn't My OnCollisionEnter2D Code Not Working?

Hi all,

This is my first question, and I know it's a basic one. I've just started my long and painful journey into the world of programming, and I've encountered an issue where I believe I have the right code, but it's not having the desired effect (it's doing nothing at all).

Game object A (which is poop) is falling, hits Game object B (a cart) which is the player. I want to destroy Object A on collision with B, increment a variable by one, then print to console the variable to ensure its working. However using the below code which I attach to Object B, object A simply hits the cart and sits on top of it without being destroyed.

Object A has a 2d sphere collider which is not a trigger and a non-kinematic rigidbody, I've tried with rigidbody collision option set to discrete and continuous. Object B (player cart) has a non-kinematic rigidbody and 2DBoxColliders which are not triggers.

Help would be much appreciated. :)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     static int poopHeld; // static variable for how much poop the player cart has currently caught/is holding.
     public float speed; // variable for controlling player movement speed.
     Rigidbody2D rb2d;   // rigidbody2d variable of player.
     
     // Use this for initialization
     void Start ()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal"); //for retrieving player movement left and right.
 
         Vector2 movement = new Vector2(moveHorizontal, 0f); //new vector2 object for player movement
 
         rb2d.AddForce(movement * speed); //player movement calculation
         
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.name == "Poop") //if statement to detect if other object is the Poop.
         {
             Destroy(other.gameObject);
             poopHeld++;
             print(poopHeld);
         }
     }   
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zoogyburger · Feb 08, 2016 at 06:14 PM

Did GameobjectB have 2 2DBoxColliders or just 1 2DBoxColliders? You have to make sure the colliders on both objects are able to hit each other properly to work. All your code worked fine except the last bit:

 void OnCollisionEnter2D (Collision2D other){
 
     if (other.gameObject.name == "Poop") 
     {
         Destroy (other.gameObject);
         poopHeld++;
         Debug.Log("Poop held");
     }
 }

You should endeavor to tag GameobjectA as "Poop" and change the code to

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

so that you don't worry about keeping the name the same for instance if you make copy GameobjectA the editor will rename it "Poop1" whick will make your code not work.

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 Phosphorus2500 · Apr 05, 2016 at 01:41 PM 0
Share

Hi Zoogy,

Thanks for your answer. I never did figure out why it wasn't working, but ins$$anonymous$$d set up an additional collider in a different position and set it as a Trigger, then used OnTriggerEnter2D and this seemed to work perfectly.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GameObject not destroying on Collision 1 Answer

Restrict held object movement 0 Answers

Jitter with Tilemap Collider's but no Jitter on standard Box Collider 1 Answer

Freeze rotation of just the box collider component 0 Answers

Flying character rigidbody - can't see any static collider 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