2021年1月7日星期四

How to sort a GameObject array using a float array

I'm making a racing game, and am to the point where I'm detecting the players "place" in the race (1st, 2nd, etc.), but the positions will update at certain checkpoints throughout the race, similar to splits in irl racing. I have a float[] for the laps completed by each of the cars (the bots in the game share a script with the player), and a GameObject[] to input what cars' positions are being measured. In a for loop, the float[] ("lapNum"), is Set to the corresponding GameObject[]'s lapCount (calculated in another script). I was able to sort the float[] with no issues, but Now, is there any way to find the GameObject that was originally used to calculate the float? Alternatively, is there any way to sort the GameObject[] to match the float[]? My code is as follows:

using System;  using UnityEngine;  using TMPro;    public class Places : MonoBehaviour  {      public float[] lapNum;      public GameObject[] racers;      [Space]      public float place;      public int carNum;      [Space]      public GameObject carmover;      public TextMeshProUGUI yourPlace;          private void Start()      {          lapNum = new float[racers.Length];      }      private void Update()      {          //finding lap Count          for (int i = 0;  i < racers.Length; i++)          {              lapNum[i] = racers[i].GetComponent<CarMove>().laps;          }            //Sorting          Array.Sort(lapNum);            //detecting place #(I need to search the corresponding GameObject, because the float[] won't be able to find a gameobject)          place = Array.IndexOf(lapNum, carmover);            //printing the place #          yourPlace.text = place.ToString();         }  }  

Thank you for your time!

https://stackoverflow.com/questions/65622919/how-to-sort-a-gameobject-array-using-a-float-array January 08, 2021 at 11:08AM

没有评论:

发表评论