I have searched like the whole Internet for this lol (get Value of specific Prefab(clone))

Hey,

so i’m working on a racing Game in which you Race against AI’s.

Every car in the game (incl. the players one) has a carEngine script attached to it, which for example makes the car accelerate as soon as the race starts.

Also the carEngine script has a piece of code measuring the time it took a car to get from start to finish Line.

Obviously when finishing the race i want to display a table with your own and your rivals (AI’s) racetimes.

The thing is, these AI’s are all identical Clones (instantiated from a Prefab when the race begins) with identical carEngine scripts.

Now the question… whats the best way to access, collect and store all these Individual racetimes from these AI Clones carEngine scripts?

I intentionally did not include any code snippets as I’m not in need for a fitted solution but rather a general understanding on how to access and differentiate values of multiple Identical GameObjects.

I know, i could give all these AIs individual names when Instantiating them but that just doesn’t feel like the best/ most efficient way to do it :confused: Correct me on that if you think its fine.

Things I googled, that didn’t help me, mostly because everyone always asks for fitted solutions:

unity get value from ai

unity ai communications

unity access script of instantiated object clone

unity access script of instantiated object

unity referencing prefab clones

unity access script of specific clone

I have basically the same problem except with block clones and fixed joints. In your case however you could have a float variable on the race car prefab, increase it by like .01 every 100th of a second (when the race starts), and end it when the car touches a trigger at the finish line (all this being in the car’s code).

When you instantiate an AI clone, add them to a list, like: public List<carEngine> drivers . The list will contain all AI drivers. Once the race ends, iterate through the list to access each AI’s race time. If you wish to store the times of each driver, you can create a dictionary public Dictionary<carEngine, float> raceTimes; I used a float but you would put whatever type you use to store your race time. Now, each AI driver is the key and their race time is the value. You can use this to create your scoreboard.

You’d access the carEngine script inside the loop like this:

foreach ( var ai in drivers )
{
    // This is how you access the variable for the time it took a car to get from start to finish Line from each AI driver.
    ai.RaceTimeVariable
}

Make sure you initialize the list before you add the AI cars to it…