Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by cj31387 · Oct 14, 2012 at 01:16 AM · prefabunique

How to find all transforms of cloned prefabs?

Is it even possible to uniquely identify all the transforms of the same prefab but use their copied transforms uniquely? For instance say I have 4 prefab clones in a scene that each have 4 clone child empty game objects. Is it possible to get every child's transform from each cloned prefab and use them uniquely? More specific, say prefab 1 has 4 empty game objects called spawn1, spawn2, spawn3, spawn4, but then there are 3 more prefabs exactly the same with the same names but you want to use JUST prefab 3's spawn4 and not effect the other prefabs spawn4. How would you do that.

Comment
Add comment · Show 2
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 Fattie · Oct 14, 2012 at 05:39 AM 1
Share

here's a full and total explanation

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

please vote it up ! :)

avatar image cj31387 · Oct 14, 2012 at 11:02 PM 0
Share

Is there a similar C# version somewhere? Some of that I can't translate.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by speedything · Oct 14, 2012 at 01:59 AM

I'm not 100% certain on what you want, but does something like this work?

 void FindSpawn4()
 {
    GameObject cloneToUse = Instantiate(prefab, position, rotation) as GameObject;
    
    foreach (Transform child in cloneToUse.transform)
    {
       if (child.name == "spawn4")
       {
          // do whatever here
       }
    }
 }
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 aldonaletto · Oct 14, 2012 at 02:56 AM

You must have a reference to the prefab you want to use, then find the desired child with transform.Find.
The way you get the reference depends on what you're trying to do. Supposing that you've selected the prefab with a raycast, for instance, you could find a child of it this way:

 if (Physics.Raycast(ray, out hit)){
   Transform spawn4 = hit.transform.Find("Spawn4");
   ...

Another possibility is to use GetChild:

   Transform spawn0 = hit.transform.GetChild(0);
   ...


Comment
Add comment · Show 1 · 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 cj31387 · Oct 14, 2012 at 03:19 AM 0
Share

well I've been stuck on this for about a week. I have the same prefab cloned with a Formation_AI script on it. I have my fighters go to one slot in the formation then the next and so on, but if the same prefab is cloned ($$anonymous$$ore than 1 formation) they wont go to the next formation prefab, they only go to the first 1, then when the first one is full they just stop going to any. I just need to know how to list or find all the transforms of ALL the CLONED prefabs in the scene so I can tell my fighters to go to the next ones.

avatar image
0

Answer by kblood · Feb 06, 2013 at 12:40 PM

Not sure if it is possible for your game, but if you could give all the enemies a tag calling them enemy, you could use the tag to target them. This code is part of my Space Invaders clone, and I target the enemies, which are all clones, by their tag:

         for(var theEnemy : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
         {
             theEnemy.transform.position.x += 2     * moveDirection;
             invadersAlive = true; // This invader is still alive
             if (theEnemy.transform.position.x > 50) changeDirection = true;
             if (theEnemy.transform.position.x < 0) changeDirection = true;
             if (theEnemy.transform.position.y < -24) gameOver = true;
             if (Random.Range(0, 100) > 95)
             {
                 // Instantiate the projectile at the position and rotation of this transform
                 var clone : Rigidbody;
                 clone = Instantiate(projectile, theEnemy.transform.position - Vector3(0, 2, 0), theEnemy.transform.rotation);
                 
                 // Give the cloned object an initial velocity along the current 
                 // object's Z axis
                 clone.velocity = transform.TransformDirection (Vector3.down * 10);
             }
         }

the Instantiate is where I make some of the enemies shoot. You could also use this to add them all to an array and control them from there, but I do not think you would even need to do so, since this already makes an array of them. Problem is if the other game objects also create the exact same prefabs. The simple but messy solution might be to just make a different prefab for each if that is possible.

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Script is working incorrectly. (rotation) 1 Answer

How can I tell what prefabs a child object (script) is in? 2 Answers

Distance betwen two objects.Error,bug,etc. 2 Answers

Two exact objects behaving differently... *scratches head* 0 Answers

Setting Position of Spawned Prefab 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges