Search Unity

2D: Setting "Ignore Tag" on Cinemachine Collision Impulse Source throws NullReferenceException

Discussion in 'Cinemachine' started by codeimpossible, Oct 5, 2018.

  1. codeimpossible

    codeimpossible

    Joined:
    Mar 27, 2013
    Posts:
    9
    Hopefully this is the right place to report a bug.

    I added a CinemachineCollisionImpulseSource to an object and set it's Layer Mask and Ignore Tag under "Trigger Object Filter". Whenever the impulse source was triggered it would throw a NullReferenceException.

    The stacktrace pointed me to line 126 in CinemachineCollisionImpulseSource.cs, in the GenerateImpactEvent method:

    Code (CSharp):
    1. 118         private void GenerateImpactEvent(Collider other, Collider2D other2d, Vector3 vel)
    2. 119         {
    3. 120             // Check the filters
    4. 121             if (!enabled)
    5. 122                 return;
    6. 123             int layer = (other != null) ? other.gameObject.layer : ((other2d != null) ? other2d.gameObject.layer : 0);
    7. 124             if (((1 << layer) & m_LayerMask) == 0)
    8. 125                 return;
    9. 126             if (m_IgnoreTag.Length != 0 && other.CompareTag(m_IgnoreTag))
    10. 127                 return;
    11. 128
    12. 129             // Calculate the signal direction and magnitude
    13. 130             float mass = GetMassAndVelocity(other, other2d, ref vel);
    14. 131             if (m_ScaleImpactWithSpeed)
    15. 132                 mass *= vel.magnitude;
    16. 133             Vector3 dir = Vector3.down;
    17. 134             if (m_UseImpactDirection && !vel.AlmostZero())
    18. 135                 dir = -vel.normalized;
    19. 136
    20. 137             // Fire it off!
    21. 138             GenerateImpulse(dir * mass);
    22. 139         }
    on line 123 there is a check done to see if the collider is a 3d or 2d collider, and then the layermask is checked against whichever collider has been passed. That check isn't done for the ignore tag. The ignore tag only checks against 3d colliders, so when using it with 2d colliders the method errors out (since "other" will be null).

    This is in cinemachine v2.2.7.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    @codeimpossible Hey thanks for reporting that! You're absolutely right. Fixed in the upcoming CM 2.2.8