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);
}
}
}
}