Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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.

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

Detecting collision on an object and not its sister object. 1 Answer

Inconsistant onTrigger AND button is pressed calls!? 1 Answer

Detect Collision in an If Statement? 1 Answer

activate gui on trigger enter 1 Answer

Trigger collision not working on Unity PSM 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