• 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 Torqus · Sep 01, 2017 at 04:41 AM · tagontriggerenter2dother

OnTriggerEnter2D works but other.tag doesn't

I have a script that adds outlines to objects when the player collides with them. The OnTriggerEnter2D works but I want to make it so the Player is the only one who triggers the outlines, as soon as I add an "if" check with other.tag, wathever is inside doesn't work anymore. This is the code:

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player")
     {
         AddOutline();
     }
 }

The Player has the "Player" tag and the script containing that code is in the Objects the player collides with.

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 Torqus · Sep 01, 2017 at 10:27 AM 0
Share

Now in another script it has a different effect.

     void OnTriggerStay2D(Collider2D other)
     {
         if (other.tag == "Player")) {
             if (this.gameObject == GameObject.FindGameObjectWithTag("Player").GetComponent<OutlineObjectsPlayer>().closestObj)
             {
                 onTriggerEnter = true;
                 AddOutline();
             }
             else
             {
                 onTriggerEnter = false;
                 RemoveOutline();
             }
         }
     }

This is supposed to, every frame the player is in the object collider, check if the triggerer is a player, check if the object is the closest to the player, and then add the outline.

This time the code works but it doesn't care if the triggerer is the player, other collisions add the outline as well. I don't even know anymore.

1 Reply

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

Answer by Torqus · Sep 02, 2017 at 08:07 AM

Turns out the collider needs the "Is Trigger" check... EDIT: Nah the problem was I can't use OnCollider and OnTrigger if my layer collider matrix ignores both colliders. The solution was to add a "Triggerer" layer and a "Triggerer" box collider that collides with everything as a child of my Player. I find it weird that nobody talks about this in tutorials and stuff. Should I not be doing this?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

113 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

Related Questions

Check if there's no GameObject with specific tag in the field (in the camera view) 0 Answers

tagged objects problem 0 Answers

i don't know why tag option is dosen't work 0 Answers

confusion on OnTriggerEnter(Collider collision) 0 Answers

Having problem with scoring system., 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