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 ForgeStudios · Sep 13, 2013 at 02:44 AM · transformchildrenaccessjavascript-specific

Getting positions of all children in a different gameObject

I've be searching for a way to get access to children of a separate object, so that I can have their positions, but the only method I could find regarding getting multiple children from one transform was: // Moves all transform children 10 units upwards!

 for (var child : Transform in transform) {
     child.position += Vector3.up * 10.0;
 }

So is there a way to rephrase this to get transforms from other objects? I am looking to do something like this:

 for (var child : Transform in transform) {
     objectPositions[i] = child[i].position;
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Danmietz · Sep 13, 2013 at 03:22 AM

GetComponentsInChildren would probably help.

I'm not certain of the JavaScript syntax, but it'd probably be something along the lines of

 var otherTransforms : Transform[];
 otherTransforms = otherObject.GetComponentsInChildren(Transform);
 for (var trans : Transform in otherTransforms)
 {
     // Do things with each transform
 }

would probably do the trick

Comment
Add comment · Show 9 · 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 vexe · Sep 13, 2013 at 03:28 AM 0
Share

I don't see a difference between your code, and:

 for (var child : Transform in transform) {
    // do something
 }
avatar image Danmietz · Sep 13, 2013 at 03:37 AM 1
Share

Sorry, I updated the example.

If you call GetComponentsInChildren on an object with a given type, it will recursively find all of the components in all of the children of that object and return it. with this, you now have called it on a separate object, as well as storing it for use elsewhere (in otherTransforms).

avatar image vexe · Sep 13, 2013 at 03:46 AM 0
Share

Ahhh wire! :D - now you got it right. Just tested it out, works fine. @ForgeStudios here's your answer. And if you're having trouble assigning stuff in for loops, go with this:

 var otherTransforms : Transform[];
 otherTransforms = otherObject.GetComponentsInChildren(Transform);
 for (var i = 0, len = otherTransforms.Length; i < len; i++) {
   var child = otherTransforms[i];
   objectPositions[i] = child.position;
 }
avatar image ForgeStudios · Sep 13, 2013 at 11:09 AM 0
Share

@vexe trying to return the children to the Transform array returns this: InvalidCastException: Cannot cast from source type to destination type. SaveLoadData.collectData () (at Assets/UnitySerializer/Scripts/SaveLoadData.js:113) Pause$$anonymous$$.EscapeWin (Int32 windowID) (at Assets/UnitySerializer/Scripts/Pause$$anonymous$$.js:157) UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/GUI.cs:1324)

avatar image vexe · Sep 14, 2013 at 03:35 AM 1
Share

The problem was the line:

 var otherTransforms = terrain.GetComponentsInChildren(Transform) as Transform[]; 

After this, otherTransforms is null - You can easily avoid that problem using the generic version GetComponentsInChildren < Transform > () - In my C# code I tested out the generic, no problem, I also tested out this, no problem as well:

 var otherTransforms = GetComponentsInChildren(typeof(Transform));


$$anonymous$$ess around with GetComp till you get it right, I advise you to debug and see the return values your self, if you're on $$anonymous$$onodev, or VS.

Show more comments

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

18 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

Related Questions

Rotate transform without rotation the children 1 Answer

Transform [] = all children of this game object ? (Spawnpoints) 3 Answers

Does a child transform take the name of its parent in unity 3D? 1 Answer

How to get all children gameobjects 5 Answers

How to move the parent object to a child of one of its children? 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