I am developing a top-down, 2d space sim in which I would like the player to see a list of planets, ships, etc. in which they are in close proximity to as well as their distances. I would like this list to be kept up to date and sorted from closest objects to farthest. This should be familiar to anyone who has played space sims such as Eve Online.
The way I am currently doing this, is by running through a loop of all gameobjects I want to see on the list and building a column (list) of gameobjects that contain text components showing their name and distance from the player ship. This is done at the beginning of each frame, and then they are deleted at the end of the frame thus keeping the list up to date. However, deleting and instantiating all the game objects each frame to form this list doesn’t run well. Here is my code:
void Update()
{
// DELETE OLD LIST
for (int i = contentGO.transform.childCount - 1; i >= 0; i--)
{
GameObject childObject = contentGO.transform.GetChild(i).gameObject;
Destroy(childObject);
}
for (int i = 0; i < entries.Length; i++)
{
if (entries[i] != null)
{
GameObject newGO = Instantiate(entryNameGO);
newGO.transform.SetParent(contentGO.transform);
int locY = -10 - (i * 10);
newGO.transform.localPosition = new Vector2(0, locY);
newGO.GetComponent<TextMeshProUGUI>().text = "Name: " + pois[i].gameObject.name;
}
}
}
Thank you for any help.
John Powell