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 :/ 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
You didn't search the whole internet. 30 seconds of searching and I found these three that fit together:
https://answers.unity.com/questions/260100/instantiate-as-a-child-of-the-parent.html
https://answers.unity.com/questions/205391/how-to-get-list-of-child-game-objects.html
https://docs.unity3d.com/ScriptReference/Transform.html
When you instantiate the clones, parent them all to an object, just an empty gameobject at world origin or whatever, doesn't matter, the parent won't move or rotate and wont affect how they work, however for purpose of organization you can get all children of that object. This gives you a list of Transforms. All you then need to do is loop through each of the objects in that array and on each of them get the game object and the component from it via GetComponent:
foreach (Transform child in storageObject) {
carEngine script = child.GetComponent<carEngine>();
// Do whatever with the script
}
The storage object can be found either by GameObject.Find("Name") or by having a public or serializable field of type GameObject and setting the object in the inspector.
Answer by PhoneguyHELLOHELLO · Jan 13, 2018 at 12:59 PM
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).
Answer by ransomink · Jan 13, 2018 at 02:29 PM
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...