using UnityEngine; using UnityEngine.UI; // This line is necessary to use UI components like Button using System.Collections; public class ObjectSelector : MonoBehaviour { private GameObject currentlySelectedObject; public Button deleteButton; // Ensure this is assigned in the Unity inspector public Camera arCamera; void Update() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { Ray ray; if (arCamera != null) ray = arCamera.ScreenPointToRay(Input.GetTouch(0).position); else ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // Debug to see what object is hit Debug.Log("Hit " + hit.collider.gameObject.name); if (hit.collider.gameObject == gameObject) { currentlySelectedObject = gameObject; deleteButton.gameObject.SetActive(true); // Show the delete button deleteButton.onClick.AddListener(() => DeleteSelectedObject()); } } } } void DeleteSelectedObject() { if (currentlySelectedObject != null) { Debug.Log("Deleting object: " + currentlySelectedObject.name); //currentlySelectedObject.gameObject.SetActive(false); Destroy(currentlySelectedObject); // Destroy the object currentlySelectedObject = null; // Clear the reference deleteButton.gameObject.SetActive(false); // Hide the delete button deleteButton.onClick.RemoveListener(DeleteSelectedObject); this.gameObject.GetComponent().DeleteObject(); } } }