Timing/Threading Issue

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. :slight_smile:

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!

One reason it may be off is that you’re looping through all your racers as a collection to see if any in that collection have finished instead of individually monitoring them.

In a race of 8 racers you have a lineup of

 [0,1,2,3,4,5,6,7]

In your case,what happens if multiple racers finish in the space of one frame? As in, get within .5 units of your finish line?

Say racers 7,6,5,4,3, 2, 1 and 0 finished in that order. By using this for loop, you’ll get the result that matches the original lineup-

 [0,1,2,3,4,5,6,7]

What would be a better thing to do is do the update checking on each racer individually so as soon as it crosses the line, it is added to the list of those that have finished rather than looping through them all.

Not sure if that’s the entire problem but hope that helps.

1 Like

@HolBol you’re correct. We switched to using a finished time property to sort them after everyone was done. Our other problem is I assumed that getting back game objects would be in a certain order. We now sort them in reverse order since -5 is closer to the top of the screen than 5. Both of those fixes combined resolved the problem. I normally only work on this on the weekends which is why it took so long to reply.

1 Like