Я бы просто сохранил их все в списке (и обычно использовал бы if-else
или, может быть, даже switch-case
для лучшей производительности)
public List<GameObject> winners = new List<GameObject>();
private void OnTriggerEnter(Collider other)
{
switch(other.gameObject.tag)
{
case "Bot1":
Debug.Log("Bot1 Finished");
currentTimeBot1 = timer;
Debug.Log(currentTimeBot1.ToString());
break;
case "Bot2":
Debug.Log("Bot2 Finished");
currentTimeBot2 = timer;
Debug.Log(currentTimeBot2.ToString());
break;
case "Bot3":
Debug.Log("Bot3 Finished");
currentTimeBot3 = timer;
Debug.Log(currentTimeBot3.ToString());
break;
case "Bot4":
Debug.Log("Bot4 Finished");
currentTimeBot4 = timer;
Debug.Log(currentTimeBot4.ToString());
break;
case "Bot5":
Debug.Log("Bot5 Finished");
currentTimeBot5 = timer;
Debug.Log(currentTimeBot5.ToString());
break;
case "Player":
Debug.Log("Player Finished");
currentTimePlayer = timer;
Debug.Log(currentTimePlayer.ToString());
break;
default:
return;
}
// this is only matched if one of the before tags matched
winners.Add(other.gameObject);
}
Таким образом, вы не только получаете первого победителя(winners[0]
), но на самом деле вы можете получить весь порядок, в котором они закончили, например,
for(var i = 0; i< winners.Count; i++)
{
Debug.LogFormat("{0}. {1}", i + 1, winners[i].name);
}
Если вам также нужно время окончания, а не использовать словарь, такой как
public Dictionary<GameObject, float> winners = new Dictionary<GameObject, float>();
private void OnTriggerEnter(Collider other)
{
switch(other.gameObject.tag)
{
case "Bot1":
winners.Add(other.gameObject, timer);
break;
}
// ...
Debug.Log(other.gameObject.tag + " Finished");
Debug.Log(winners[other.gameObject].ToString());
}
снова получите доступ к каждому из них, например
var winner = winners[0].key;
var bestTime = winners[0].value;
, или выполните итерацию по всем победителям
foreach(var kvp in winners)
{
var winner = kvp.key;
var finishTime = kvp.value;
//...
}
или получите доступ к определенному времени окончания GameObjects
var finishTime = winners[aCertainGameObject];