So, i have a problem with destroying a gameObject. I need to destroy a gameObjects with same tag that colliding with eachother but only when i press left mouse button and only ones that colliding with object that i clicked on. It sounds very complliceted…
void Update()
{
PlayerInput();
}
void PlayerInput()
{
if (Input.GetMouseButtonDown(0))
{
CastRay();
}
}
void CastRay()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
if (hit)
{
Destroy(hit.collider.gameObject);
}
}
}
Code destroying only one gameObject when i click on it.