I'm trying to make a clicker game. The score is supposed to go up when the player hits the dinosaur, but nothing should happen when they hit nothing or any other object.
Only the dinosaur has a collider.
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Vector2 raycastPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(raycastPos, Vector2.zero);
if (hit.collider.name == "Dinosaur" )
{
ChangeDinoSize(dinoScaleChange);
scriptUI.AddScore(1);
}
}
It works as long as I click the dinosaur, but anything outside of the dinosaur's collider gives me the NullReferenceException.
When I give other objects a collider, it works as it should - nothing happens when I click them. But is there a better way, so I don't have to add colliders to everything?