I'm trying to get an enemy to turn around when a wall is in front, for which I set a raycast to detect if the collider name was a wall. When trying to test the code, it shoots the error "Object reference not set to an instance of an object". I don't see the problem.
here is the code
void WallDetection()
{
RaycastHit2D wallRay = Physics2D.Raycast(wallDetect.position, Vector2.right, 4f);
if (wallRay.collider.name == "Wall Right")
{
moveRight = false;
}
else if (wallRay.collider.name == "Wall Left")
{
moveRight = true;
}
↧