• 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 unity_R0Zoro · Mar 31, 2020 at 08:47 PM · collisiontriggerdestroy objectobject reference2d collision

Ball wont destroy in collision even though that the ball are public GameObject

Sorry I am Bad in English. I have 6 color bar and ball The ball doesn't destroy when collide it go through the color BAR

     public GameObject Ball;
      void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject == Ball) 
         {
             Destroy(col.gameObject);
         }
     }

![alt text][1]

untitled.png (146.2 kB)
untitled.png (146.2 kB)
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

3 Replies

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

Answer by twrabetz · Mar 31, 2020 at 09:05 PM

Hello,

If you instantiate a prefab, Unity will create "clones" of it, which might not actually register as the same GameObject. You should do as Curve's answer suggested:

1- Go to your Ball prefab and in Tags at the top of the inspector, click the dropdown and select the "Add Tag.." option. Add a new PurpleBall tag and then select that new tag from the dropdown.

2- In your code, check if (col.gameObject.CompareTag("PurpleBall")) Destroy(col.gameObject).

Comment
Add comment · Show 2 · 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 unity_R0Zoro · Apr 01, 2020 at 06:59 AM 0
Share

Thanks, man you made my day <3

avatar image FlaSh-G · Apr 01, 2020 at 07:27 PM 0
Share

If I might add this here: http://blog.13pixels.de/2019/why-strings-are-not-your-friend/

avatar image
0

Answer by Codester95 · Mar 31, 2020 at 08:59 PM

Give this a try :)

 public GameObject Ball;
       void OnTriggerEnter2D(Collider2D col)
      {
       if(col.tag == "TargetTag"){
            Destroy(col.gameObject);
       }
      }






Comment
Add comment · 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
0

Answer by FlaSh-G · Mar 31, 2020 at 09:24 PM

The ball you collide with is not the same object as the prefab, so your comparison evaluates to false. There is also no way that you can ask Unity "which prefab is this object an instance of?".

What you have to do is: Make a new script that contains some means of identification. I'll use an enum as an example, but there are many ways to do this - which is better is depending on your situation.

 public class BallType : MonoBehaviour
 {
   public enum BallColor
   {
     Blue, Gold, Green, Purple, Red, White
   }
 
   // public field for simplicity
   public BallColor color;
 }

You put this on each of your ball prefabs and set the right color on each.

Your collision script then also has a BallColor property and checks against that in OnCollisionEnter:

 public BallType.BallColor collidingColor;
 
 private void OnCollisionEnter(Collision collision)
 {
   var ballType = collision.gameObject.GetComponent<BallType>();
 
   if (ballType && ballType.color == collidingColor)
   {
     Destroy(collision.gameObject);
   }
 }
Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

How to perform a smooth push in the opposite direction of a wall 0 Answers

Temporarily ignore collisions between player and enemies 2 Answers

If trigger hit, spawn it 1 Answer

create event on collision 2 Answers

Collecting Array items in order 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges