Building Placement Issue: Buildings Fly to camera and back to the floor

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

public class BuildingPlacementSystem : MonoBehaviour
{
public List buildingPrefabs;
public List silhouettePrefabs;
public List placedBuildingCount = new List();

public int currentBuildingIndex = 0;
public int bedNumber;
public int totalBedCount;
public int buildingWoodCost;
public bool isPlacing;
private bool canRotateBuilding = true;
private bool isDragging;
private bool isCancelling;
private float rotationSpeed = 50f;
public string buildingType;
private GameObject currentBuilding;
private GameObject currentSilhouette;
//public List<GameObject> silhouettePrefabsAvaliable;
//public List<GameObject> silhouettePrefabsUnavaliable;
private Vector3 targetPosition;

//public PlacedBuilding bSPlacedBuilding;
public ResourceManager bSResourceManager;

//public int rayLength;

void Start()
{

        BuildingPlacementSystem[] buildingSystems = FindObjectsOfType<BuildingPlacementSystem>();

        Dictionary<string, int> buildingCounts = new Dictionary<string, int>();

        foreach (BuildingPlacementSystem system in buildingSystems)
        {
            if (system.isPlacing)
            {
                string buildingType = system.buildingType;

                if (buildingCounts.ContainsKey(buildingType))
                {
                    buildingCounts[buildingType]++;
                }
                else
                {
                    buildingCounts[buildingType] = 1;
                }
            }
        }

        foreach (var entry in buildingCounts)
        {
            string buildingType = entry.Key;
            int count = entry.Value;

            Debug.Log($"Building type {buildingType} has {count} instances.");
        }
}


void Update()
{
    if (isPlacing)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        //Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);

        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            targetPosition = hit.point;

            if (isDragging)
            {
                currentBuilding.transform.position = targetPosition;
            }

            UpdateCurrentSilhouette();
        }

        if (Input.GetMouseButtonDown(1))
        {

            CancelBuilding();
        }

        if (!isDragging && !isCancelling)
        {
            if (Input.GetMouseButtonDown(0))
            {

                PlaceBuilding();
            }
        }

        if (Input.GetMouseButtonUp(0) && isDragging)
        {

            ReleaseBuilding();
        }


        if (isPlacing && currentSilhouette != null && canRotateBuilding == true)
        {
            if (Input.GetKey(KeyCode.Q))
            {
                currentSilhouette.transform.Rotate(Vector3.up, -rotationSpeed * Time.deltaTime, Space.World);
            }
            if (Input.GetKey(KeyCode.E))
            {
                currentSilhouette.transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime, Space.World);
            }
            //Debug.Log("döndür");
        }

        foreach (PlacedBuilding placed in placedBuildingCount)
        {
            //Debug.Log($"Building type: {placed.buildingType}, Count: {placed.count}");
        }

    }
}

void UpdateCurrentSilhouette()
{
    // Store rotation
    Quaternion silhouetteRotation = Quaternion.identity;
    if (currentSilhouette != null)
    {
        silhouetteRotation = currentSilhouette.transform.rotation;
        Destroy(currentSilhouette);
    }

    GameObject currentSilhouettePrefab = silhouettePrefabs[currentBuildingIndex];
    currentSilhouette = Instantiate(currentSilhouettePrefab, targetPosition, silhouetteRotation);
    //Debug.Log("UpdateCurrentSilhouette çalıştı");
}



void PlaceBuilding()
{

    canRotateBuilding = false;
    GameObject currentBuildingPrefab = buildingPrefabs[currentBuildingIndex];
    // Use the rotation from the silhouette to instantiate the building
    currentBuilding = Instantiate(currentBuildingPrefab, targetPosition, currentSilhouette.transform.rotation);
    isDragging = true;
    UpdateCurrentSilhouette();
    //Debug.Log("PlaceBuilding çalıştı");

    string buildingType = currentBuildingPrefab.name;

    PlacedBuilding placed = placedBuildingCount.Find(b => b.buildingType == buildingType);

    if (placed != null)
    {
        placed.count++;
        placed.bedCount += GetBedCountForBuildingType(buildingType);
        buildingWoodCost = GetBuildingsWoodCostForBuildingType(buildingType);
    }
    else
    {
        int bedCount = GetBedCountForBuildingType(buildingType);

        PlacedBuilding newPlaced = new PlacedBuilding
        {
            buildingType = buildingType,
            count = 1,
            bedCount = bedCount
        };
        placedBuildingCount.Add(newPlaced);
    }

    //Debug.Log("PlaceBuilding çalıştı");
}

int GetBuildingsWoodCostForBuildingType(string buildingType)
{

    Dictionary<string, int> buildingWoodCost = new Dictionary<string, int>
{
    { "BasicEv", 5 },
    { "ComplexEv", 10 },
    { "Tree", 1 }
};

    if (buildingWoodCost.ContainsKey(buildingType))
    {
        return buildingWoodCost[buildingType];
    }
    else
    {
        // Return a default value if the type is not found
        return 0;
    }
}

int GetBedCountForBuildingType(string buildingType)
{

    Dictionary<string, int> bedCounts = new Dictionary<string, int>
{
    { "BasicEv", 2 },
    { "ComplexEv", 5 },
    { "Tree", 1 }
};

    if (bedCounts.ContainsKey(buildingType))
    {
        return bedCounts[buildingType];
    }
    else
    {
        // Return a default value if the type is not found
        return 0;
    }
}

int CalculateTotalBedCount()
{
    int total = 0;

    foreach (var placed in placedBuildingCount)
    {
        total += placed.bedCount;
    }

    return total;
}

void ReleaseBuilding()
{
    if (currentSilhouette != null)
    {
        Destroy(currentSilhouette);
    }

    bSResourceManager.ReduceWood();

    isPlacing = false;
    isDragging = false;
    isCancelling = false;
    canRotateBuilding = true;
    //Debug.Log("ReleaseBuilding çalıştı");
    totalBedCount = CalculateTotalBedCount();

}

void CancelBuilding()
{
    if (currentSilhouette != null)
    {
        Destroy(currentSilhouette);
    }

    if (isDragging && currentBuilding != null)
    {
        Destroy(currentBuilding);
    }
    isPlacing = false;
    isDragging = false;
    isCancelling = false;
    //Debug.Log("CancelBuilding çalıştı");
}

public void StartPlacingBuilding(int buildingIndex)
{
    if (bSResourceManager.wood >= buildingWoodCost)
    {
        if (buildingIndex >= 0 && buildingIndex < buildingPrefabs.Count)
        {
            currentBuildingIndex = buildingIndex;
            isPlacing = true;
        }
        //Debug.Log("StartPlacingBuilding çalıştı");
    }
}

}

[System.Serializable]
public class PlacedBuilding
{
public string buildingType;
public int count;
public int bedCount;
}

The problem I face is that when I try to place a building using your script, the building constantly moves closer to the camera and then back to the floor. The script was working just fine until I added colliders to the prefabs. How can I solve this problem?

the problem is my ray hit the collider of the object that I was placing so this solved
int layerMask = 1 << LayerMask.NameToLayer(“Building”);