Race Standings Help.

Hey everyone,
I am starting to feel like a liability on this forum but this issue is such a big one for me and I just cant overcome it. I am trying to make it so that you can see your standing during the race, 1st, 2nd ect but I just cannot write it.
I came across an old thread: Logic on calculating positions in a race. - Questions & Answers - Unity Discussions of which I followed the answer but it gives the error, Input string was not in the correct format. I had a quick scout on Google but could not find the specific issue for this. If anyone could help that would be great.

Full error:
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
System.Convert.ToInt32 (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:1270)
Car.OnTriggerEnter (UnityEngine.Collider other) (at Assets/My Scripts/Car.cs:20)

The Code, this script is put on each car and everything is tagged correctly (“1” for the first waypoint, “2” for the second etc) but when I pass a trigger that is when it spams the Input string was not in the correct format error.

using UnityEngine;
using System.Collections;

public class Car : MonoBehaviour {
    public int currentWaypoint;
    public int currentLap;
    public Transform lastWaypoint;
   
    private static int WAYPOINT_VALUE = 100;
    private static int LAP_VALUE = 10000;
   
    // Use this for initialization
    public void Initialize() {
        currentWaypoint = 0;
        currentLap = 0;
    }
   
    public void OnTriggerEnter(Collider other) {
        string otherTag = other.gameObject.tag;
        currentWaypoint = System.Convert.ToInt32(otherTag);
        if (currentWaypoint == 1) // completed a lap, so increase currentLap;
            currentLap++;
        lastWaypoint = other.transform;
    }
   
    public float GetDistance() {
        return (transform.position - lastWaypoint.position).magnitude + currentWaypoint * WAYPOINT_VALUE + currentLap * LAP_VALUE;
    }
   
    public int GetCarPosition(Car[] allCars) {
        float distance = GetDistance();
        int position = 1;
        foreach (Car car in allCars) {
            if (car.GetDistance() > distance)
                position++;
        }
        return position;
    }
}

This code goes on the _RaceManager gameobject and I put each car into the carObjects array as instructed.

using UnityEngine;
using System.Collections;

public class RaceManager : MonoBehaviour {
    public GameObject[] carObjects;
    public Car[] allCars;
    public Car[] carOrder;
   
    public void Start() {
        // set up the car objects
        allCars = new Car[carObjects.Length];
        carOrder = new Car[carObjects.Length];
        for (int i = 0; i < carObjects.Length; i++) {
            allCars[i] = carObjects[i].GetComponent<Car>();
        }
    }
   
    // this gets called every frame
    public void update() {
        foreach (Car car in allCars) {
            carOrder[car.GetCarPosition(allCars) - 1] = car;
        }
    }
}

Thanks in advance and I am so sorry if I sound stupid as this problem (making a race position script) has been probably my worst experience with Coding yet.

The error seams to come from the conversion from string to int. personnaly, i prefer using int.parse(string). It may not be the solution, but it may be enough :slight_smile:

Thanks, I will try it out.

Ok, I fixed the error it was wrongly names tag.
Now that it is fixed I have another issue. I dont know how to view the car’s position either by Debug Logging or by putting it on a text object. Any help with getting me to display the positions would be great!

Debug.Log(gameObject.name + ": " + transform.position);

Putting that on each object will log the name and the position. As for the text object, same proecure-- TextMesh.text will help. Though in some cases you may need to convert the Vector3 into a string.