How to prioritise detection of Button click in RTS building placement using raycast

Hello,

I’m trying to create a building placement mechanic for my RTS game. The way it roughly works is by clicking a Button to activate or deactivate the building placement mechanic & using ScreenPointToRay(Input.MousePosition) to detect where to place the building.

If the raycast is within the correct building zone, the previewed building will turn to color green, if it is not within the correct building zone, the previewed building will turn red.

My problem right now is when i want to click the Button to deactivate the building placement mechanic while the raycast is within the correct building zone, it will deactivate the placement mechanic and spawn a building, which I don’t want to happen.

So how do I prioritize detecting the button click first so that it will onlydeactivate the placement mechanic, even if the raycast is within the correct building zone?

My code is as follows:

	void Update ()
    {
        //Selected == True : Preview the building 
        //Selected == False : Stop previewing the building
        if (selected)
        {

        //NOTE : GameObject troop represents the prefab for the building, which is used for previewing where to build and instantiating the actual building.

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

            if (Physics.Raycast(ray, out hit, rayDistance))
            {

                if (hit.collider.tag == "SpawnArea")
                {
                    troop.transform.position = hit.point;
                    troop.GetComponent<Renderer>().sharedMaterial.color = Color.green;

                   
                }
                else if(hit.collider.tag == "NotSpawnArea")
                {
                    troop.transform.position = hit.point;
                    troop.GetComponent<Renderer>().sharedMaterial.color = Color.red;
                }
                

            }
            //If left-click is pressed when the previewed building is within the correct building zone
            if (Input.GetMouseButtonDown(0) && troop.GetComponent<Renderer>().sharedMaterial.color == Color.green)
            {
                Debug.Log("SPAWN BUILDING");
                GameObject spawnTest = Instantiate(troopPrefab, troop.transform.position, Quaternion.identity);
                spawnTest.GetComponent<Renderer>().material.color = Color.white;
            }

            Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.blue);

        }
        else if(!selected)
        {
            troop.SetActive(false);
        }

    }

//Used on built-in OnClick function for the Button in the Inspector, for activating and deactivating building placement mechanic

public void SelectTroop()
{
    if (!selected)
    {
        selected = true;
    }
    else if(selected)
    {
        selected = false;
    }
}

Any help is appreciated.

it triggers the raycast when you press the ui button. The easiest hotfix is to call EventSystem.current.IsPointerOverGameObject () to check if the tap is on a ui element. But the Best way is to use PhysicsRaycaster so that everything is based on EventSystem.