I'm using Raycast2D to detect ground and project a shadow (see code below). My problem is the error message that seems to appear when the ray doesn't hit anything *"NullReferenceException: Object reference not set to an instance of an object PlayerController.Update () (at Assets/Scripts/PlayerController.cs:175)"*. It messes with the rest of code so that the player sometimes don't die.
If I could I would just get rid of the float distance "10f" but without it the shadow seems to disappear when the player moves towards the ground in high speed.
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 10f);
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Ground") && dead == false)
{
shadow.SetActive(true);
shadow.transform.position = hit.point;
}
else
{
shadow.SetActive(false);
}
↧