So I have this script attached to 8 different objects in a scene, one is the player.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HealthScoring : MonoBehaviour {
public float health;
public float score;
public Rigidbody rigidBody;
public Text scoreText;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Racer" || collision.gameObject.tag == "Player")
{
score += rigidBody.velocity.magnitude * 25;
health -= rigidBody.velocity.magnitude * 2;
}
}
void Update()
{
scoreText.text = score.ToString ("0");
if (health <= 0)
{
health = 0;
Destroy (gameObject, 0.5f);
if (gameObject.tag == "Player")
{
//call end of race here
}
}
}
}
What I need to do, once the player is dead, is retrieve the score float from all the objects in the scene that has the previous script attached to it. Here’s my end race script, I’m pretty sure a lot of it is wrong.
public Canvas raceEndCanvas;
public HealthScoring scoring;
public GameObject playerObj;
public GameObject racer;
public int[] scores;
void Start()
{
scoring = GetComponent<HealthScoring>();
racer = GameObject.FindGameObjectsWithTag ("Racer");
playerObj = GameObject.FindGameObjectWithTag ("Player");
}
public void EndOfRace()
{
for (int i = 0; i < scores; i++)
{
// compare, sort scores
}
}
Now the problem I’m having is how do I get the individual score float from each Racer / Player game object in the scene, compare them and sort them highest to lowest? And do I need another array to store the number of Racer game objects in the scene?