Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vallar · Apr 03, 2016 at 08:53 AM · c#collidertriggerdestroy

Destroying Object OnTriggerEnter2D not working

Hello guys,

I have trying to resolve this problem for a very long time and I seriously can't figure out what to do to fix this. I have the below code on a object with a Box Collider 2D set to Trigger:

 public Object CollectableDestroyedEffect;
     public Object BallDiesEffect;
     public Object EnemyDestroyedEffect;
 
     private string objectTag;
 
     void OnTriggerEnter2D(Collider2D other)
     {
         objectTag = other.gameObject.tag;
 
         switch (objectTag)
         {
             case "Enemy":
                 other.GetComponent<EnemyCollisions>().DestroyEnemy(true, false);
                 break;
             case "Collectable":
                 Instantiate(CollectableDestroyedEffect, other.transform.position, Quaternion.identity);
                 Destroy(other);
                 break;
             case "Ball":
                 //Destroy(other);
                 Instantiate(BallDiesEffect, other.transform.position, Quaternion.identity);
                 GameManager.instance.SpawnBall();
                 break;
             case "Projectile":
                 Instantiate(EnemyDestroyedEffect, other.transform.position, Quaternion.identity);
                 Destroy(other);
                 break;
             //default:
             //    Instantiate(CollectableDestroyedEffect, other.transform.position, Quaternion.identity);
             //    Destroy(other);
             //    break;
         }
     }

What I am having problem with is the tag "Collectable".

I made sure the tag is correct, I made sure all of the objects have a RigidBody2D, and I made sure that they are on the same plane and same layer. The weird thing is that the effects are instantiated correctly (so the Instantiate code) runs and I can see, thing is, the objects are getting destroyed at all.

Worthy of note is that the Collectable tagged objects have Box Collider 2D and set to trigger.

Any idea how I can fix this?

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

1 Reply

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

Answer by Kurtisi · Apr 03, 2016 at 09:03 AM

Well as i understand you want to destroy collectable after instantiating some effect when you enter it's trigger zone..

Have you tried managing this in several if statements?

 if(other.tag=="Collectable")
  {
       Instantiate(CollectableDestroyedEffect, other.transform.position, Quaternion.identity);
       Destroy (other.gameObject); //or Destroy(other);
 }
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 Vallar · Apr 03, 2016 at 09:40 AM 1
Share

Well that actually fixed it!

other.gameObject was what I am missing ins$$anonymous$$d of other only. I don't understand it much, but I am guessing if I do Destroy(other) only, I am only destroying the collider (as other is of type Collider2D and not gameObject)?

I didn't change it into an if statement though, any reason why I would use if statements ins$$anonymous$$d of a Switch? Just curious that is all.

Thanks a lot $$anonymous$$urtisi, you are awesome!

avatar image Kurtisi Vallar · Apr 04, 2016 at 06:42 AM 0
Share

Yes, that is exactly the reason because you destroy collider only.

I suggested if statements just because it's easier for me like that way :P

avatar image Vallar Kurtisi · Apr 04, 2016 at 07:09 AM 0
Share

Gotcha, thanks a lot $$anonymous$$urtisi! :)

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

126 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

Related Questions

Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers

C# OnTriggerEnter Issue 3 Answers

OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers

How to get the Collider other element Rigidbody? 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