Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 daX-_-_908 · Jan 22 at 10:30 PM · triggerbeginnernot workingtag

Trigger not working with a specific game object

Hello everyone, I have a problem with a trigger. Basicaly I want the the trigger to work only when the player pass on it. But if an enemy collide with it or a bullet collide with it, it gets activated. I tried to put a tag on the player but it doesn't seems to work because the trigger doesn't recognize the player or the bullet shooted from his gun. Here is the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class StartTrigger : MonoBehaviour
 {
     public GameObject Player;
 
     public GameObject Spawns;
 
     public GameObject Trigger;
 
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             Spawns.SetActive(true);
         }
         
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         Trigger.SetActive(false);
     }
 
Comment
Add comment · Show 2
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 bluethunder3991 · Jan 23 at 07:50 AM 1
Share

Usually , when I face issues with triggers not detecting the proper gameobject , it was always my fault . Sometimes I add the tag and forget to assign the tag to the Gameobject . Sometimes the Trigger and Tag are in different parts of the hierarchy.

avatar image daX-_-_908 bluethunder3991 · Jan 23 at 08:24 AM 0
Share

I had already assigned the tag to the player. Now I've tried to assign the same tag to the trigger but it dosn't work anyway. And yes, right now the Trigger and the Player are in different part of the hierarchy. Do you think it's a problem?

1 Reply

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

Answer by Chimer0s · Jan 23 at 08:22 AM

The tag should definitely work. Is "Trigger" the gameObject with this script on it? Because currently anything that exits this trigger will cause that to be turned off. If you want to make sure that nothing besides the player will interact with this collider, the most foolproof way is to place the collider on a layer that doesn't interact with any layers besides the one the player is on. That way nothing else will be able to trigger collisions with it. For example, you create a layer called "Player" and another one called "Player Detection." Then you place the player on the Player layer and this trigger on the Player Detection layer. In your physics settings, you go to the collision matrix and uncheck all the other layers from the list of those that can interact with player detection so only the box for the player layer is checked.

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 daX-_-_908 · Jan 23 at 08:56 AM 0
Share

Yes the Trigger is "tirgger". I've created a layer for the Trigger (the player has already a layer called "player") and deleted the part of the code with compare tag but it doesn't work because the bullet of the player's pistol interact with the trigger even if it is on a different layer. I've also unchecked all the other layers apart from the player layer.

[1]: /storage/temp/191584-immagine-2022-01-23-095546.png

Do I have to also write someting in the code?

immagine-2022-01-23-095546.png (22.5 kB)
avatar image Chimer0s daX-_-_908 · Jan 23 at 09:08 AM 1
Share

There has to be something configured incorrectly then. It's not possible for a layer that isn't the Player to interact with the InteractableTrigger layer if you have it configured the way it is in that image. Either the bullet is also on the Player layer(or maybe has a child collider on that layer) or the trigger isn't on the InteractableTrigger layer. The only other possibility I could think of is if your bullet is using a raycast and manually calling ontriggerenter on anything it hits instead of it actually being a collider being detected, but I'm sure you would have mentioned that if it were the case. If everything is 100% configured correctly, try restarting Unity. It's caused me some headaches over the years when I was sure I had everything right and it wasn't working. I'd waste 2 hours trying to trouble shoot the bug then just restart Unity and it would work as expected.

avatar image daX-_-_908 Chimer0s · Jan 23 at 09:32 AM 0
Share

I'm dumb, I've changed the layer on the wrong trigger and obiusly it doen't work. Now I've the doubt that maybe is the same thing for the Tag. Thank you very mutch for your help.

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

158 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

Related Questions

How to make two objects collide using "Is Trigger" 1 Answer

Do a collider only with certain taged objects 2 Answers

Use trigger with player but have a collider with everything else? 3 Answers

OnTriggerEnter2D randomly stops detecting 1 Answer

OnTriggerEnter not working on terrain in 3.5 beta 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