• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
-1
Question by leonabels1998 · Dec 22, 2017 at 08:02 PM · gameobjectprefabscloneaccessclones

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DreadKyller · Dec 23, 2017 at 10:39 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My gameobjects keep changing layers 1 Answer

Meteorite Fall [English/German] Meteoritenschauer 0 Answers

my instantiation /prefab of dan wont play death animations like orignal dan 0 Answers

How can I destroy a prefab clone? 1 Answer

Why my clone gameobject cannot move? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges