Why is Get Component Failing On Component Known To Exist?

So, I’m firing a ray at my object. It’s returning the object fine. It hits every debug.log but the last. I’m not sure why it would be behaving like this.

private void EventDispatcher_RayHitEvent(WeaponRayHitEventArgs args)
{
    Debug.Log("Dispatching Ray Hit Event..");
    m_ProcessedDamage = 
        Mathf.Abs(m_BaseEnergyRayDamage - args.m_RaycastHit.distance) / Mathf.PI;
    if(args.m_RaycastHit.collider.gameObject != null)
    {
        m_CurrentRayHitObject = args.m_RaycastHit.collider.gameObject;
        Debug.Log("Collision Detected..");

        if (m_CurrentRayHitObject.GetComponent<PlayerState>() != null)
            Debug.Log("Player State Detected!");
    }
}

The same logic works perfectly fine upon detecting collision, just not ray hits…

void CheckCollisionForPlayerState()
{
    if (m_Collision.collider.gameObject.GetComponent<PlayerState>() != null)
    {
        Debug.Log("Object With Player State Hit!");
        m_Collision.collider.gameObject.GetComponent
            <PlayerState>().Damage(m_ProjectileBaseDamage * m_ProjectileBaseDamage);
    }
}

The component --definitely-- exists.

        if (GetCurrentWeapon().GetOwnEmission() == BaseMaterialWeapon.EmissionType.Beam &&
        Physics.Raycast(transform.position + m_ForwardVector, m_ForwardVector, out m_RaycastHit))
        {
            EventDispatcher.InvokeRayHit(new WeaponRayHitEventArgs(m_RaycastHit));
            Debug.Log(string.Format("Object Hit With Weapon Ray: ({0})", 
            m_RaycastHit.collider.gameObject.name));
        }

You clearly don’t reference the same object. Keep in mind that if your “collider” is on a child object a raycast that hits that collider will return that collider. GetComponent will only search on that exact gameobject that has the collider attached.

Try using a Debug.Log like this in both cases:

Debug.Log(string.Format("Object Hit With Weapon Ray: ({0})", 
    m_RaycastHit.collider.gameObject.name), m_RaycastHit.collider);

Note the additional second parameter to “Log”. When you pass a UnityEngine.Object reference there, that object will be “pinged” in the hierarchy when you click once on the debug message in the console. So you can clearly see which object you’re actuallly using.

You may want to use GetComponentInParent which does the same as GetComponent, but it also searches up the hierarchy for the component. GetComponentInChildren does the same but down the hierarchy.

You have abstracted several things here, so we don’t know when you call those methods, what those variables are and how they are initialized / what they contain.

It seems strange to have parameters of an event seemingly stored in a member variable (specifically “m_Collision”). This looks like you misuse a member variable for passing information to methods. You should use method arguments here. Storing event bound data could cause the strangest behaviour. The collision data is only relevant for the collision event itself. Unless the class you’re using is for handling a single collision event, the member variable seems just wrong.

As a final statement: If GetComponent returns “null” you can be sure that there is no such component on that object.

So, get this. I didn’t have a collider on my object. Hahahahaha.