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

Destroy Gameobject without BoxCollider2D

Hi, I have this code:

the script works with objects with boxcollider2D components but there are some objects who doesn't have, is there a way to destroy everything 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
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
Best Answer

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

This 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 this but I don't think there is alternative. This is a primitive way to check collision, used mostly when people create their own program without too much api.

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 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
1

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
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 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 ins$$anonymous$$d 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.

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

How to destroy and trigger on enter. 1 Answer

How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer

OnTriggerEnter Influencing all GameObjects of the Same Type 2 Answers

Why won't the coin destroy when hit? 0 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