Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Dollar-Fish · Jul 26, 2015 at 05:22 PM · collisionunity 2dcollision2dgame object

If gameobject on collision with gameobject then destroy

I want to make it so when I attack and the attackTrigger collision thing is on collision with a watermelon for example, it will destroy that watermelon. My attackTrigger appears for 0.2 seconds when I attack but when it does appear, I want what gameobject it is on collision with, to be destroyed. My attackTrigger box collider has a is trigger, and the fruit does not have a is trigger. My character has a rigid body. This script doesn't work, when it is on collision, it doesn't destroy the fruit. How can I make this work script work and destroy the gameobject? Thank you in advance.

 using UnityEngine;
 using System.Collections;
 
 public class attackTrigger : MonoBehaviour {
 
     public GameObject watermelon;
     public GameObject strawberry;
     public GameObject grape;
     public GameObject banana;
     public GameObject orange;
     public GameObject tomato;
     public GameObject pineapple;
     public GameObject apple;
     public GameObject blueberries;
     public GameObject raspberries;
     public GameObject pear;
     public GameObject blackberries;
     public GameObject lemon;
 
     void OnTriggerEnter2D(Collider2D col)
     {
 
         if(col.isTrigger != true && col.gameObject == watermelon)
         {
             Destroy (watermelon);
         }
         if(col.isTrigger != true && col.gameObject == strawberry)
         {
             Destroy (strawberry);
         }
         if(col.isTrigger != true && col.gameObject == grape)
         {
             Destroy (grape);
         }
         if(col.isTrigger != true && col.gameObject == banana)
         {
             Destroy (banana);
         }
         if(col.isTrigger != true && col.gameObject == orange)
         {
             Destroy (orange);
         }
         if(col.isTrigger != true && col.gameObject == tomato)
         {
             Destroy (tomato);
         }
         if(col.isTrigger != true && col.gameObject == pineapple)
         {
             Destroy (pineapple);
         }
         if(col.isTrigger != true && col.gameObject == apple)
         {
             Destroy (apple);
         }
         if(col.isTrigger != true && col.gameObject == blueberries)
         {
             Destroy (blueberries);
         }
         if(col.isTrigger != true && col.gameObject == raspberries)
         {
             Destroy (raspberries);
         }
         if(col.isTrigger != true && col.gameObject == pear)
         {
             Destroy (pear);
         }
         if(col.isTrigger != true && col.gameObject == blackberries)
         {
             Destroy (blackberries);
         }
         if(col.isTrigger != true && col.gameObject == lemon)
         {
             Destroy (lemon);
         }
 
     }
 
 }
 
Comment
Add comment · Show 8
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 Dollar-Fish · Jul 26, 2015 at 01:25 PM 0
Share

It is hitting the fruit for sure but it is not sending any messages.

avatar image Hexer · Jul 26, 2015 at 05:25 PM 0
Share

If it is a collision, you want to make sure to use OnCollisionEnter2D and Collision2D ins$$anonymous$$d of OnColliderEnter2D

avatar image Dollar-Fish · Jul 26, 2015 at 07:26 PM 0
Share

That sadly didn't work, but I made it work a different way, but I want to check gameobject rather than tag. Could I somehow fix this?

 using UnityEngine;
 using System.Collections;
 
 public class attackTrigger : $$anonymous$$onoBehaviour {
     
     public GameObject watermelon;
     public GameObject strawberry;
     public GameObject grape;
     public GameObject banana;
     public GameObject orange;
     public GameObject tomato;
     public GameObject pineapple;
     public GameObject apple;
     public GameObject blueberries;
     public GameObject raspberries;
     public GameObject pear;
     public GameObject blackberries;
     public GameObject lemon;
     
     void OnTriggerEnter2D(Collider2D col)
     {
         
         if(col.isTrigger != true && col.CompareTag("Fruit"))
         {
             Destroy (watermelon);
             Debug.Log("Hello");
         }

avatar image Hexer · Jul 26, 2015 at 07:51 PM 0
Share

In your old script you checked the gameobject ins$$anonymous$$d of tag...

avatar image maccabbe · Jul 27, 2015 at 12:26 AM 3
Share

If you want to destroy the gameObject you are colliding with then you should probably just destroy the gameObject you are colliding with. i.e.

  void OnTriggerEnter2D(Collider2D col) {         
      if(col.isTrigger != true) {
           Destroy(col.gameObject);
      }
  }
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Unity2D: How to make my player not walk through walls? 1 Answer

How to optimize a lot of colliders? 1 Answer

Why do two colliders cross at high force? 0 Answers

2d game end level with trigger and colission with trigger who to make a if condition 1 Answer

2D Checkpoint Collide not block 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