Hopefully this is just me being silly but if not, I can post even more source code. The basic problem is that I have a list of items that are racing to an ending. I want to know who finished and in what order so here’s the code that should do that. My problem is that the final list seems to be in a random order. Any thoughts, theories, suggestions are appreciated. Also, links to threads showing me how woefully mistaken I am are also appreciated. ![]()
This script is attached to the scene at loading and has the following… Also, if someone can tell me how to format code I’ll edit this. I just googled and failed. Tonight is not my night apparently.
// Update is called once per frame
void Update() {
if (isRacing) {
updateSnails();
} else if (raceFinished) {
raceFinished = false;
isRacing = false;
handleResults();
}
}
Basically the above says keep having my snails race until the race is done.
private void updateSnails() {
for(int i=0; i < racingSnails.Count; i++) {
if (!racingSnails.finished) {
racingSnails.move();
if (racingSnails.finished) {
finishedSnails.Add(racingSnails);
Debug.Log(racingSnails.snailName + "finished");
racingSnails.Remove(racingSnails);
}
}
}
if (racingSnails.Count == 0) {
isRacing = false;
raceFinished = true;
}
}
In move the snail updates it’s finished flag if it’s within a certain distance of the end point. I found setting a hard distance doesn’t work because it might update while it’s not exactly on the end point. The code essentially does something like…
*_</em></em></em></em> <em><em><em><em><em>*Vector3 endPosition = endPoint.transform.position; Vector3 currentPosition = snail.transform.position; float dist = Vector3.Distance(endPosition, currentPosition); if (dist < 0.5) { finished = true; snail.GetComponent<Animator>().speed = 0; }*</em></em></em></em></em> <em><em><em><em>_*
Edit: I read the stickies and found the code tag! Woot!