'How to spawn only one object in Unity AR foundation

I new for Unity and C# can someone help me? I'm creating a kids game and they tend to tap on the screen accidentally and as they look around but then they spawn the object again and its over the first one. I just want when i touch screen object was ones spawned. Sory for my English. Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ProgrammManager : MonoBehaviour
{
    [Header("Put your planeMarker here")]
    [SerializeField] private GameObject PlaneMarkerPrefab;

    private ARRaycastManager ARRaycastManagerScript;
    private Vector2 TouchPosition;
    
    public GameObject ObjectToSpawn;
    void Start()
    {
        ARRaycastManagerScript = FindObjectOfType<ARRaycastManager>();

        PlaneMarkerPrefab.SetActive(false);
    }

    
    void Update()
    {
       
        ShowMarker();
        

    }


    void ShowMarker()
    {
        List<ARRaycastHit> hits = new List<ARRaycastHit>();

        ARRaycastManagerScript.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);

        if (hits.Count > 0)
        {
            PlaneMarkerPrefab.transform.position = hits[0].pose.position;
            PlaneMarkerPrefab.SetActive(true);
        }
        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
           
            Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
        }
    }
}


Solution 1:[1]

If you want to spawn your object only once, you can add a boolean check into your script.

private bool objectSpawned = false;

Then set your bool to true when you spawn the object.

        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    {
       
        Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
        objectSpawned = true;
    }

Now, you insert your check into the Update() function. This way, if you already spawned an object, you won't be able to spawn any more.

void Update()
{
   if(!objectSpawned)
   {
    ShowMarker();
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KBaker