Im getting a null reference exception and i have no idea why.
void Shoot ()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, range, canShoot);
{
if (hit != null)
{
hit.collider.gameObject.SendMessageUpwards("HitMe", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
its happening on the line that says
hit.collider.gameObject.SendMessageUpwards();
What if you change if (hit != null)
to “if (hit.collider.gameObject)
”, does it ever hit, and does the error go away?
I have never used SendMessageUpwards,
so I don’t really know its peculiarities. But I regard SendMessage as a sketchy hack.
The best solution would be to figure out what failure of design has made the use of a SendMessageUpwards
call necessary, and fix that so that you already know exactly what script you are calling “HitMe” on.
Or is “damage” null? That could also be your problem.