• 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
Question by arturoza · Aug 21, 2014 at 01:22 AM · gameobjectdestroyontriggerenter

Destroy Gameobject without BoxCollider2D

Hi, I have t$$anonymous$$s code:

the script works with objects with boxcollider2D components but there are some objects who doesn't have, is there a way to destroy everyt$$anonymous$$ng the destroyer touches, Thanks for your help:

 using UnityEngine;
 using System.Collections;
 
 public class Destroyer : MonoBehaviour 
 {
 
     void OnTriggerEnter2D(Collider2D Other)
     {
         if(Other.gameObject.tag == "Untagged");
         {
             Destroy(Other.gameObject);
         }
     }
 }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by revolute · Aug 21, 2014 at 02:32 AM

T$$anonymous$$s means you will have to do every calculation by your own script. All objects have transform components, so you will have to calculate the distance between objects check if they collide. I'm guessing you don't want to do t$$anonymous$$s but I don't t$$anonymous$$nk there is alternative. T$$anonymous$$s is a primitive way to check collision, used mostly when people create their own program without too much api.

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 arturoza · Aug 21, 2014 at 02:43 AM 0
Share

Hi thanks for your answer, I guess I'll have to find another way since the speed isn't constant

avatar image

Answer by alebasco · Aug 21, 2014 at 01:40 AM

That code should work with any 2D colldier.

However, there is no way to check collision with an object that does not have a collider. Unity does not know they are colliding, and OnTriggerEnter is never called. As no collider has entered the Destroyer's collider.

Why not give the other objects that you need to destroy colliders?

Comment
arturoza

People who like this

1 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 arturoza · Aug 21, 2014 at 01:51 AM 0
Share

Hi, thanks for your anwser, I cant add a boxcollider since I want to delete a background which keeps spawning (like an infinite runner) and then dissapear of the screen, is there a way to achieve it without a boxcollider2D?

avatar image alebasco · Aug 21, 2014 at 05:33 AM 1
Share

You could add it as a box collider with a trigger.

Alternatively, if you are spawning more and more backgrounds - don't delete them! Once they go off screen, move them to the front of the line instead of spawning new ones. That will be more efficient as well. To check to see if an object is visible, try renderer.isVisible. (Note, if ANY camera can see it, then it will be true. This includes scene view)

avatar image arturoza · Aug 21, 2014 at 12:55 PM 1
Share

Thanks! that's a really good idea, I'll try both of them. thanks for your help alebasco.

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.

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

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

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

Why won't the coin destroy when hit? 0 Answers

OnTriggerEnter Influencing all GameObjects of the Same Type 2 Answers

How to only delete one of two collided objects? 1 Answer

How come when the player runs into the coin it won't destroy? 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