i ve 15 objects in my scene, and when using raycast it takes the first objects near the camera, even if you clike the last objects it will destroy the first one. here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SecondD : MonoBehaviour
{
Ray _ray;
RaycastHit _raycastHit;
Camera _camera;
// Start is called before the first frame update
void Start()
{
_camera = Camera.main;
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
ShootRay();
}
}
private void ShootRay()
{
_ray = _camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(_ray, out _raycastHit))
{
Destroy(GameObject.FindWithTag("Objects"));
}
}
}