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 Kensei · Apr 05, 2014 at 03:48 PM · c#2dcollidertriggerdestroy

I can't get 2DCollider to work.

I have object spawning at random locations that fall down, all of them have rigidbodies attached. I've created a zone where I want anything entering it be destroyed in order to clean all the leftover clones that bypass the player. Here's my code.

 enter code hereusing UnityEngine;
 using System.Collections;
 
 public class Cleaner : MonoBehaviour 
 {
 
     void OnTriggerEnter2D (Collider2D other)
     {
         
         Destroy(other.gameObject);
         Debug.Log ("Cought");
     }
 
 }
 

I've tried having both the zone and the objects set as triggers, tried both collision and trigger enter functions, tried both having rigidbodies...but to no avail. I just want the trigger zone to destroy anything that goes through it.

I've added a Debug.Log and it looks like this code doesn't even register that something is passing through it.

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 AlucardJay · Apr 05, 2014 at 03:52 PM 1
Share

First step is to always Debug to see if the collider is working. Add this above the Destroy line :

 Debug.Log( gameObject.name + " trigger entered by " + other.gameObject.name );
avatar image Owen-Reynolds · Apr 05, 2014 at 05:36 PM 0
Share

So, maybe better results redoing the question with title "Can't get 2DTrigger to work?"

avatar image Kensei · Apr 05, 2014 at 05:53 PM 0
Share

Point taken.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by origonn · Apr 05, 2014 at 05:55 PM

Your parameter is a Collider2D instead of a Collision2D.

With the OnTriggerEnter2D, is one of the colliders set to be isTrigger?

Comment
Add comment · Show 5 · 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 Kensei · Apr 05, 2014 at 06:03 PM 0
Share

Thanks for the collider help. Yes, I've tried setting it to be trigger-trigger and trigger-collider.

avatar image origonn · Apr 05, 2014 at 06:06 PM 0
Share

For the trigger, the OnTriggerEnter2D method will be called in the object that is set to isTrigger when a collider enters it. So if you want your collidable object to dissapear when it enters a zone, have that zone's collider set to isTrigger, have the object entering it to be a regular collider, and the OnTriggerEnter2D method will be triggered in the zone script and must be present there, not in the script of the object being destroyed

avatar image Kensei · Apr 05, 2014 at 06:32 PM 0
Share

Oh my god...that's exactly how I set it up, however your isTrigger tip made me check the collider in the inspector again...and it just happened so that I forgot to add that script to the object...so embarrassing.

avatar image Owen-Reynolds · Apr 06, 2014 at 03:36 PM 0
Share

Hmmm...in 3D, you need the same set-up: a collider, a trigger, and a proper OnTriggerEnter. But OnTriggerEnter can be on either object's script (or both and called for each.)

I don't use 2D. Are you sure OnTriggerEnter2D must be on the trigger?

avatar image Kensei · Apr 13, 2014 at 02:30 PM 0
Share

I don't think it matters where the script is attached for it to work, you just have to attach it to the object that you want to have the specific behavior. Worked for me.

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

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

Related Questions

Collision With Trigger, Then Add Points To Counter, Then Destroy 1 Answer

OnMouseDrag issue, Event System 0 Answers

Triggers Interacting with Triggers 0 Answers

C# Invalid Points Assigned to 2D Edge Collider 1 Answer

Sword Kills Enemies From Far Away 3 Answers


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