I want to destroy and prefab that is clicked on, but when I click in the object other prefabs are also destroyed. I’ve looked into getting an instance ID of the clicked on object and using it, but I do not know how to implement that. Thanks in advance for any help.
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hitInfo = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
if (hitInfo)
{
Debug.Log(hitInfo.collider.gameObject.name);
GameObject.Destroy(gameObject);
}
}