Problem with script (Space scanning)

Hi! I’m new in programming, trying to make scanner of different object in radius on various layers around player (similar as in EVE Online). My idea is to assign Layers to gameObjects, use Physics.OverlapSphere to detect colliders. Then add each object (that was met) with collider to Lists. Then in UI i create ObjectSlots for each object in list, then show them in UI. However, it does not work correctly, showing only two objects form Planet Layer. I checked almost everything i could, also tried show separate lists in UI such as only Asteroids Layer list, but it did not work. Could you help me and show mistakes? Here is my script:

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

public class UI_ShipScanner : MonoBehaviour
{
    [SerializeField] private Transform slotParent;
    [SerializeField] private GameObject slotPrefab;

    public LayerMask whatIsAsteroid;
    public LayerMask whatIsPlanet;
    public LayerMask whatIsAnomaly;
    public LayerMask whatIsWreck;
    public LayerMask whatIsStar;
    public LayerMask whatIsBlackhole;
    public LayerMask whatIsShip;
    public LayerMask whatIsStation;
    public LayerMask whatIsBeacon;
    public LayerMask whatIsGates;
    public LayerMask whatIsPost;

    public float scanRadius = 2000f;

    private List<Transform> detectedTargetsAsteroid = new List<Transform>();
    private List<Transform> detectedTargetsPlanet = new List<Transform>();
    private List<Transform> detectedTargetsAnomaly = new List<Transform>();
    private List<Transform> detectedTargetsWreck = new List<Transform>();
    private List<Transform> detectedTargetsStar = new List<Transform>();
    private List<Transform> detectedTargetsBlackhole = new List<Transform>();
    private List<Transform> detectedTargetsShip = new List<Transform>();
    private List<Transform> detectedTargetsStation = new List<Transform>();
    private List<Transform> detectedTargetsBeacon = new List<Transform>();
    private List<Transform> detectedTargetsGates = new List<Transform>();
    private List<Transform> detectedTargetsPost = new List<Transform>();

    [SerializeField] private List<Transform> allDetectedTargets = new List<Transform>();

    private List<UI_ObjectSlot> objectSlots = new List<UI_ObjectSlot>();

    private void Start()
    {

        RefreshCheck();
    }

    void Update()
    {

        ScanForTargets(whatIsAsteroid, detectedTargetsAsteroid);
        ScanForTargets(whatIsPlanet, detectedTargetsPlanet);
        ScanForTargets(whatIsAnomaly, detectedTargetsAnomaly);
        ScanForTargets(whatIsWreck, detectedTargetsWreck);
        ScanForTargets(whatIsStar, detectedTargetsStar);
        ScanForTargets(whatIsBlackhole, detectedTargetsBlackhole);
        ScanForTargets(whatIsShip, detectedTargetsShip);
        ScanForTargets(whatIsStation, detectedTargetsStation);
        ScanForTargets(whatIsBeacon, detectedTargetsBeacon);
        ScanForTargets(whatIsGates, detectedTargetsGates);
        ScanForTargets(whatIsPost, detectedTargetsPost);

        allDetectedTargets = CombineDetectedTargets();

        UpdateScannerUI();
    }

    private void ScanForTargets(LayerMask layerMask, List<Transform> detectedTargetsList)
    {
        detectedTargetsList.Clear();
        Collider[] targets = Physics.OverlapSphere(transform.position, scanRadius, layerMask);
        foreach (Collider target in targets)
        {
            Transform targetTransform = target.transform;
            detectedTargetsList.Add(targetTransform);
        }
    }
    private List<Transform> CombineDetectedTargets()
    {
         List<Transform> combinedTargets = new List<Transform>();
         combinedTargets.AddRange(detectedTargetsAsteroid);
         combinedTargets.AddRange(detectedTargetsPlanet);
         combinedTargets.AddRange(detectedTargetsAnomaly);
         combinedTargets.AddRange(detectedTargetsWreck);
         combinedTargets.AddRange(detectedTargetsStar);
         combinedTargets.AddRange(detectedTargetsBlackhole);
         combinedTargets.AddRange(detectedTargetsShip);
         combinedTargets.AddRange(detectedTargetsStation);
         combinedTargets.AddRange(detectedTargetsBeacon);
         combinedTargets.AddRange(detectedTargetsGates);
         combinedTargets.AddRange(detectedTargetsPost);
         return combinedTargets;
    }


     private void UpdateScannerUI()
     {
          RefreshCheck();

          foreach (Transform target in allDetectedTargets)
          {
                Vector3 targetPosition = target.position;
                Vector3 playerPosition = transform.position;
                float distanceToTarget = Vector3.Distance(playerPosition, targetPosition);
                ObjectScript detectedObject = target.GetComponent<ObjectScript>();

                GameObject newSlot = Instantiate(slotPrefab, slotParent);
                newSlot.GetComponent<UI_ObjectSlot>().SetupSlot(detectedObject, distanceToTarget);
          }
     }


    private void RefreshCheck()                                     
    {

        if (slotParent.childCount > 0)
        {
            for (int i = 0; i < slotParent.childCount; i++)
            {
                Destroy(slotParent.GetChild(i).gameObject);
            }
        }
    }
   
}

The code does actually look workable so guess you’ll have to debug classically: Reduce to one layer only and make sure that that works by either printing or better go with the debugger.

However I’m writing for a different reason: This aporoach will be horrible for performance! You are creating numerous arrays, the new combined list and also many UI objects every single frame!
That accumulates. Look into how you can reuse objects. That is very important for a game. “Object pooling” is an important keyword to look up.
Furthermore ther are variants of the Physics collision methods that do not alocate a new array but reuse a given one.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
    
public class UI_ShipScanner : MonoBehaviour
{
        [SerializeField] private Transform slotParent;
        [SerializeField] private GameObject slotPrefab;
    
    
        public float scanRadius = 2000f;
    
    
        private List<UI_ObjectSlot> objectSlots = new List<UI_ObjectSlot>();
    
        private void Start()
        {
            RefreshCheck();
        }
    
        void Update()
        {
            UpdateScannerUI();
        }
    

    
     private void UpdateScannerUI()
     {
          RefreshCheck();
       
          Collider[] targets = Physics.OverlapSphere(transform.position, scanRadius);
          foreach (Collider target in targets)
          {
                if (target.tag!="laser bolt")
                {
                    Vector3 targetPosition = target.transform.position;
                    Vector3 playerPosition = transform.position;
                    float distanceToTarget = Vector3.Distance(playerPosition, targetPosition);
                    ObjectScript detectedObject = target.GetComponent<ObjectScript>();
   
                    GameObject newSlot = Instantiate(slotPrefab, slotParent);
                    newSlot.GetComponent<UI_ObjectSlot>().SetupSlot(detectedObject, distanceToTarget);
                }
          }
     }
    
    private void RefreshCheck()                                   
    {
        if (slotParent.childCount > 0)
        {
            for (int i = 0; i < slotParent.childCount; i++)
            {
                Destroy(slotParent.GetChild(i).gameObject);
            }
        }
    }
      
}