How to spawn a prefab when a button with a certain tag is pressed

I’m trying to figure out how to spawn a prefab where the camera is looking and when a ui button with a certain tag is pressed. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Spawner : MonoBehaviour {
    private Vector3 _Position;
    public GameObject Prefab;
void OnEnable(Button other)
        if (other.CompareTag("Cube"))
            Instantiate(Prefab, _Position, Quaternion.identity);

   private void GetPosition()

        Vector3 mousePosFar = new Vector3(

        Vector3 mousePosNear = new Vector3(

        Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
        Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);

        RaycastHit hit;

        if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit, 1000))
            _Position = new Vector3(hit.point.x, hit.point.y, hit.point.z);


i think for a simple way u can throw away that big GetPosition() function;

just give the camera a collider like sphere collider ;

then make a bool variable to be true each time the collider has enter trigger with your object or cube and false it when it exit;

and then again when u enable just check if that bool is true so u know the camera is looking;