Unity AR (Tap and Place Object)

I want to only place one duck (prefab) but the script I used is not function well because the raycast will also detect the AR Plane. However, for my objective I just want to
-tap and place a duck
-tap again the duck will show the confirmationUI.
-and also how should I code so that the UI canvas can nicely in front of the camera and also rotation by y-axis with 180 degree.

Here is the script for tap and place object and also show UI:

void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse clicked");

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))
            {
                Debug.Log("Hit");
                Debug.Log(hit.transform.name + " " + hit.transform.tag);

                if (hit.collider.tag == "Placed Duck")
                {
                    Debug.Log("Hit");
                    Debug.Log(hit.transform.name + " " + hit.transform.tag);
                    showUI();
                    return;
                }
            }

            if (!isDuckSpawned && aRRaycastManager.Raycast(Input.mousePosition, hits, TrackableType.PlaneWithinPolygon))
            {
                foreach (ARRaycastHit ARhit in hits)
                {
                    Debug.Log("Raycast produced");
                    if (aRPlaneManager.GetPlane(trackableId: ARhit.trackableId).alignment == PlaneAlignment.HorizontalUp)
                    {
                        Pose pose = ARhit.pose;
                        Debug.Log("Object placed");

                        if (spawnedObject == null)
                        {
                            Vector3 spawnDuckPosition = new Vector3 (pose.position.x, pose.position.y + 0.03f, pose.position.z);

                            spawnedObject = Instantiate(duck, spawnDuckPosition, pose.rotation);
                            spawnedObject.tag = "Placed Duck";
                            isDuckSpawned = true;

                            Debug.Log("Duck Spawned!");
                            break;
                        }
                            
                    }

                }
            }
        }
    }

    public void showUI()
    {
        Debug.Log("ShowConfirmationUI called");

        if (canvas != null && confirmationUI != null)
        {
            Vector3 duckPosition = spawnedObject.transform.position;
            Vector3 canvasPosition = new Vector3(duckPosition.x, duckPosition.y + 0.2f, duckPosition.z - 0.1f);

            canvas.transform.position = canvasPosition;
            canvas.transform.LookAt(Camera.main.transform);
            

            confirmationUI.SetActive(true);
        }
    }

Sounds like you wrote a bug… and that means… time to start debugging!

Work through each step you list above. Do not move onto the next step until you are sure each part is working.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Once you have more information than:

… here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.