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 Fexception · Sep 04, 2014 at 07:33 AM · triggerdestroygame object

OnTriggerEnter not working

I have many objects in a 2D game that spawn on screen which are continuously travelling on the X at a certain rate. When they leave the visible area, I want them to destroy themselves, so I took someones advice and made an empty game object with a long box collider attached to it, IsTrigger is checked. However, objects pass right through this thing and they don't destroy with the following code, nor does it give me a debug message:

 using UnityEngine;
 using System.Collections;
 
 public class DeleteObjects : MonoBehaviour {
 
     void OnTriggerEnter(Collider other) {
         Destroy(other.gameObject);
         Debug.Log ("Object Destroyed");
             
 
     }
 }


All objects are on the same Z axis, so I feel like they should at least trigger the debug message. Thoughts?

Comment
Add comment · Show 2
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 dorpeleg · Sep 04, 2014 at 07:36 AM 0
Share

Can you give more information?

Are you using 2D or 3D physics?

Is this script sitting on the trigger or on the objects?

avatar image Fexception · Sep 04, 2014 at 01:34 PM 0
Share

All of the objects which I want destroyed have a Rigidbody 2D component. The game object that I'm using as the trigger does not.

I am using only 2D Physics. The trigger(with script) is sitting on an empty game object. It is not on the game objects which I want destroyed.

1 Reply

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

Answer by HarshadK · Sep 04, 2014 at 07:38 AM

For OnTrigger function to work one of the game objects must have a rigidbody attached to it. In this case your objects that you are destroying should have a rigidbody.

If you want to destroy game objects when they are no longer visible you can use MonoBehaviour.OnBecameInvisible()

Update:

Based on your recent comment, you should use OnTriggerEnter2D if you are using 2D physics.

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 Fexception · Sep 04, 2014 at 01:36 PM 0
Share

Harshad$$anonymous$$,

Can I place OnBecameInvisible on an empty game object and it will apply to all objects? Or do I need to place this on each script on each game object?

avatar image HarshadK · Sep 04, 2014 at 01:38 PM 0
Share

You need to place it on every object which you want destroyed so that it will be called when that particular object is not visible.

Also check the update in my answer.

avatar image Fexception · Sep 04, 2014 at 03:17 PM 0
Share

That worked, thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Target touching enemy collision 1 Answer

How to Disable/Enable another Script with Triggers? 1 Answer

Explosion triggers another explosion 2 Answers

Destroying objects within 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