How do I Create 5 Random Racers?

I’m having a heckuva time figuring out how to create a persistent/permanent group objects that I can select from randomly to race each other over a preset course.

Right now, my racers are primitive spheres (NavMeshAgents). Right now, I can create five or ten spheres and assign them random speed and acceleration numbers and have them race. No problem there. I know how to do all that.

But, at some point, I want to create hundreds of objects and randomly select a few each time to race over one of my courses.

I know how to create the race course and the nav mesh and obstacles and bake them and all that…absolutely no problem with any of that stuff…I already have all that in place and it works fine. I can even manually create the racers and give them random speed/acceleration and have them race…that works fine too.

BUT…

I don’t know how to create the List or Class or Group of racers. Once created, their speed and acceleration should remain the same…those should never change. The only thing that changes is that in one race, I might randomly select Racers 5, 17, and 22 to compete. In the next race, I might select 4, 13 and 26.

I’ve been looking up all sorts of stuff and I can’t seem to figure out how to accomplish this.

There are a whole bunch of ways to do this. You could for example have a scrip loop 100 times create a racer, and then add them to something like GameObject array, they will keep their initial properties, and you can just pick them by index number.

// Total number of racers you want
    int numberOfRacers = 100;
    // An array to store them all in
    GameObject[] racers;
    void MakeRacers()
    {
        // This will loop through as many times as you have set "numberOfRacers" to.
        for (int i = 0; i < numberOfRacers; i++)
        {
            racers *= // the GameObject of a racer that your other code is making with the random values etc.*

}
}

int numberOfRacersToSelect = 12;
GameObject racerToRace;
void RandomSelection()
{
// This will loop through as many times as you have set “numberOfRacersToSelect” to.
for (int i = 0; i < numberOfRacersToSelect; i++)
{
// Get a random float from 0 to the total number of racers you have, and have Mathf.RoundToInt make sure it
// is a usuable integer for our array
racerToRace = racers[Mathf.RoundToInt(Random.Range(0, numberOfRacers))];
// Note that there is nothing in place to stop you from rolling the same random number multiple times.

// CODE FOR WAHT TO DO WITH racerToRace GOES HERE
}
}



Something more efficient and flexible would be to use a C# indexers:
[Indexers - C# Programming Guide | Microsoft Learn][1]

Edit: Indexers don’t seem to be playing nice with Unity> I would use something else for now.
[1]: Indexers - C# Programming Guide | Microsoft Learn

How about creating a class with all the information stored as variables? And then have a list of that class saved. Whenever you want to select the racers, just recreate them again from the list.

Something like this?

using UnityEngine;
using System.Collections;

public class Racer: MonoBehaviour // Probably don't even need MonoBehaviour depending on what you do
{
    public float speed;
    public float acceleration;

    // This is where you are making a new car. 
    public Racer (float s, float a){
        speed = s;
        acceleration = a;
    }

}

And somewhere else, you create a list to store the Racers.

List<Racer> racers = new List<Racer>();
racers.Add( new Racer(4.0f, 5.0f));  // Obviously, you should make a for loop and use random values

And later you can reassign the values to an actual race car GameObject.

float mySpeed = racers[0].speed;
float myAccel = racers[0].acceleration;