Search Unity

  1. Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice

Resolved Can't get CompareTag to work?

Discussion in 'Scripting' started by dannyryba, Aug 19, 2020.

  1. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Hi everyone, I have tried everything I can think of to get this working.

    So I have a top down shooter I am creating with weapon pickups that change the player's shooting pattern. Right now I am using a CompareTag system to tell the player what to do if they run into a weapon pickup:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if(other.CompareTag("Enemy") || other.CompareTag("Enemy Projectile"))
    4.         {
    5.             Debug.Log("Take some damage");
    6.             DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    7.             TakeDamage(damageDealer);
    8.         }
    9.         else if(other.CompareTag("Pickup"))
    10.         {
    11.             Debug.Log("pick me up");
    12.             WeaponPickup weaponPickup = other.gameObject.GetComponent<WeaponPickup>();
    13.             ChangeWeapon(weaponPickup.weapon);
    14.             weaponPickup.PickedUp();
    15.         }
    16.     }
    Right now, the enemy and enemy projectile collisions work just fine. However, the pickup tag is never triggering its debug log entry. I have made sure it is spelled right and everything, and it's just not working. Does anyone have any ideas why this may not be working? I can of course post more of my code as well but through my testing I can't figure out where the problem would lie outside of this.

    Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    15,488
    At the start of the trigger handler, print out the tag!

    When I print strings I like to put an extra character RIGHT outside the string, to ensure I didn't have an extra space or something in the tag:

    Code (csharp):
    1. Debug.Log( "X" + other.gameObject.tag + "Y");
    On that note I would try to get away from using spaces in my tags. It probably works but to an old school guy like me it feels icky.
     
    Last edited: Aug 19, 2020
    dannyryba and PraetorBlue like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,829
    First step is to add logging like this to your code:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         Debug.Log($"OnTriggerEnter2D called. other's tag was {other.tag}.");
    4.         if(other.CompareTag("Enemy") || other.CompareTag("Enemy Projectile"))
    5.         {
     
    Last edited: Aug 19, 2020
    dannyryba and Kurt-Dekker like this.
  4. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Thanks for the advice! Makes total sense. So I went ahead and did that so I could get more information and the pickup tag is never being triggered at all. So this leads me to believe that for some reason, the pickup is not ever being registered as colliding with the player. I'm not sure why this could be.
    Here is how things are set up in my Inspector if that helps:



    I have checked my Layer Collision Matrix as well and everything seems to be good - the Player layer and Pickups layer are check to each other.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,829
    Does either object have a Rigidbody2d attached?
     
  6. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    The player has a RigidBody2D, but the pickup does not.
    The 'Enemy' and 'Enemy Projectile' tagged objects have one as well.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,829
    Is there any parent/child relationships happening with triggers and rigidbodies? Are you 100% sure you used 2D versions of colliders and rigidbodies everywhere? Does the player use a trigger collider as well, or normal collider?
     
    dannyryba and Kurt-Dekker like this.
  8. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Actually I just reviewed it and the player does not have a Rigidbody like I thought! I gave the pickup one and it work! Thanks so much for your help, of course it was something so simple... o_O

    One more question that I have in case you have any suggestions: Is there a better way of doing this rather than checking for a bunch of tags on each OnTriggerEnter2D? It seems like this is not the most efficient way of doing it, even though it works.
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,829
    I'm currently doing something similar in a project of mine. I'm using composition to solve it. In my case I created just one "Pick-up-able" component that I attach to all of the different things that can be picked up. The job of that component is just to register the pickup and fire off a C# event. I then have other components attached to the same object as the Pickupable component that listen for the C# event on that component. Then in my OnTriggerEnter, I just have to check for the existence of that one component and call a single method on it that fires off the event. The other components take care of the individual per-object-type behaviors by listening to the event. The event includes a reference to the entity that "picked it up" etc...
     
    Last edited: Aug 19, 2020
    dannyryba likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    15,488
    This is a great question and looks like Praetor has some good ideas above.

    As a software engineering professional, I work with a lot of large codebases that other people wrote, so I tend to fall back to the "oldschool" way of doing things because basically, I cannot make ANY assumptions about how other people put their projects together.

    And let me tell you, these eyes have seen some ... stuff. :)

    "How does this even WORK?!"
     
    dannyryba likes this.
  11. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Thank you both for the replies on this! Praetor that actually sounds like a great way to solve this. I have used events in the past and really like them, so I may experiment with that idea. :)
     
    Kurt-Dekker likes this.
unityunity