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 /
avatar image
0
Question by andrewwebber25 · Jul 17, 2017 at 07:15 PM · collisioncollidertrigger2d-platformertags

Making Sprite Disappear on Trigger

Hey guys so i have a sprite of a Cabin and I added a 2D box collider component to it with Is Triggered. I wrote a very small script (with no errors) that will destroy the GameObject when collided with. I also added a tag called "Cabin" which is referenced in the attached script. I have no idea why it doesnt work :( I messed around with the code a lot, switching Trigger with Collider, adding rigidbody to it and everything in between. This is like my sixth attempt at this and its really slowing me down. With Is Trigger on, he just walks right through it, with it off, he walks into it like a wall. Does anyone see what I did wrong, a setting I may have incorrect, or something in my little bit of code? Even if you have your own way of doing it in C# and would like to share, I would really appreciate it. Here is my code:

 using System.Collections.Generic;
   using UnityEngine;
   using UnityEngine.UI;
 
 
  
  public class Cabin : MonoBehaviour {
      public void OnTriggerEnter(Collider node)
      {
          if (node.gameObject.tag == "Cabin")
          {
              Destroy(node.gameObject);
          }
      }
  }
 

and here is my Cabin in Inspector:

![alt text][1]

If you need to see anything else, I will be happy to upload it! Thanks so much! [1]: /storage/temp/97860-capture.jpg

capture.jpg (123.6 kB)
Comment
Add comment · Show 1
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 carle13 · Jul 17, 2017 at 09:23 PM 0
Share

Is this script attached to the player or to the object that has the trigger?

2 Replies

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

Answer by carle13 · Jul 17, 2017 at 09:32 PM

I saw the capture you uploaded, and it seems that the problem is that the script is attached to the cabin. The OnTriggerEnter doesn't work because a trigger can't detect when something has entered it, you need to check from the object that enters the trigger. So to fix it just attach the script to the player.

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 andrewwebber25 · Jul 17, 2017 at 10:48 PM 0
Share

Alright man I'm going to give it a chance tomorrow and I'll let you know how it goes!

avatar image andrewwebber25 · Jul 18, 2017 at 01:49 PM 0
Share

I tried it out and it worked! Thank you so much! I knew it was something simple but I feel a little dumb not trying that hahah

avatar image
0

Answer by DeeCeptor · Jul 17, 2017 at 07:20 PM

OnTriggerEnter is for 3D collisions/triggers. You'll need to use OnTriggerEnter2D when using 2D colliders,

 public class Cabin : MonoBehaviour {
       public void OnTriggerEnter2D(Collider2D node)
       {
           if (node.gameObject.tag == "Cabin")
           {
               Destroy(node.gameObject);
           }
       }
   }

Comment
Add comment · Show 7 · 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 andrewwebber25 · Jul 17, 2017 at 07:35 PM 0
Share

@DeeCeptor I gave that a shot before and it didnt work again, player just walks right through the collider. Here is what I changed my code to:

 using System.Collections.Generic;
   using UnityEngine;
   using UnityEngine.UI;
 
 
  
  public class Cabin : $$anonymous$$onoBehaviour {
      public void OnTriggerEnter2D(Collision node)
      {
          if (node.gameObject.tag == "Coin")
          {
              Destroy(node.gameObject);
          }
      }
  }

I get the debugger error: Script error: OnTriggerEnter2D This message parameter has to be of type: Collider2D The message will be ignored.

I go back in my code where it says "(Collision node)" and change it to "(Collider 2D node)" and the error goes away.The player still walks through collider with nothing happening. Am I using the Collider2D in the wrong place?

avatar image DeeCeptor andrewwebber25 · Jul 17, 2017 at 07:45 PM 0
Share

The parameter needs to be Collider2D, not a Collision. View my updated answer.

avatar image andrewwebber25 DeeCeptor · Jul 17, 2017 at 08:12 PM 0
Share

Hmm, it still gives this error Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.

Show more comments

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

121 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

Related Questions

How do you execute Trigger-collider collision only in one gameobject? 1 Answer

Undefind tag? 0 Answers

How can I detect a collision point, but allow player to pass through collider. 1 Answer

How to make work a collision when is invisible? 3 Answers

Unity2D side collision detection 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