using UnityEngine;
using UnityEngine.InputSystem;

public class ClickSpawner : MonoBehaviour
{
    public GameObject spherePrefab;
    private GameObject spawnedSphere;

    void Update()
    {
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform == this.transform)
                {
                    if (spawnedSphere == null)
                    {
                        spawnedSphere = Instantiate(spherePrefab,
                            transform.position + new Vector3(0.15f, 0, 0),
                            Quaternion.identity);
                        spawnedSphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    }
                    else
                    {
                        Destroy(spawnedSphere);
                        spawnedSphere = null;
                    }
                }
            }
        }
    }
}
