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
1
Question by valtteri_m · May 02, 2015 at 07:06 PM · javascriptlistchildfindorder

Get a list of children (in order)

I would like to have a list of all the children of Player, and have all of the objects in the list have like a number or something to indicate what order they are, 1st.. 2nd.. and such, you know?

(javascript (unityscript) pls)

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 siaran · May 02, 2015 at 08:03 PM 0
Share

ordered how? by what property? You could use GetComponentsInChildren() to get all child transforms, then sort it somehow I guess?

avatar image valtteri_m · May 02, 2015 at 08:23 PM 0
Share

like, what I need this thing for is that a child looks what number it is (1st, 2nd, 3rd..) and uses it for an equation

1 Reply

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

Answer by DoTA_KAMIKADzE · May 02, 2015 at 08:44 PM

If I got you correctly I think that what you're looking for is basically in my P.S. section of my answer HERE.

Then when you find in that way every child of that object you assign them to some sort of Dictionary or whichever container and add them #1 - which indicates direct child. Then you repeat that code for every child and find their child and add them to that Dictionary with #2 (it can be int value or whatever you like, doesn't matter), and so on and so forth, if no child was found for all of them then continue with whatever you want to do with them.

I believe you'll be able to convert those few lines of code yourself to javascript, if not let me know.

P.S. Update to your updated question, here is how should you use that code that I've shown you in that link:

     Transform par = transform.parent;
     int childCnt = par.childCount;
     for (int i = 0; i < childCnt; i++)
     {
         Transform childX = par.GetChild(i);
         if (childX == transform)
         {
             //do your stuff
             // use "i" for your number
             //if you want your "i" to start from 1 then add +1 to it before you'll be doing your stuff
             break;//you can use break in the end here to break the rest of the loop, because you no longer need it from what you've specified
         }
     }

P.P.S. Here you go:

 var parent : Transform;
 var childCnt : int;
 function Start () 
 {
     parent = transform.parent;
 }
 function Update () 
 {
      childCnt = parent.childCount;
      for (var i : int = 0; i < childCnt; i++)
      {
           var childX : Transform = parent.GetChild(i);
           if (childX == transform)
           {
               //do your stuff
               break;
           }
      }
 }

Now if the parent can change as well then move line #5 to line between #8-9 or 9-10. If the number of child can't change and parent can't change then move whole thing from Update to Start function and just save your Integer value from there and then do with that integer whatever you want in your Update function.

Comment
Add comment · Show 5 · 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 valtteri_m · May 02, 2015 at 09:01 PM 0
Share

I uh.. Don't think it's what im looking for.. I want so that a child has a script and sees what place it is in as a child 1st, 2nd, 3rd.. and then uses that int in a math equation

avatar image DoTA_KAMIKADzE · May 02, 2015 at 09:22 PM 0
Share

Oh so, then that will be even easier, and yeah with that script that I've shown. I'll update my answer in few seconds - check the P.S. section, if you'll have a hard time converting to javascript let me know I'll do it but I hope I won't need to :)

avatar image valtteri_m · May 02, 2015 at 10:11 PM 0
Share

I have no knowledge on C# and "for" stuff.. heres all that I could convert :x

 var Parent : Transform = transform.parent;
 var childCnt : int = Parent.childCount;
 
 function Update(){
      for (int i = 0; i < childCnt; i++){
          var childX : Transform = Parent.GetChild(i);
          if (childX == transform){
              //do your stuff
              // use "i" for your number
              //if you want your "i" to start from 1 then add +1 to it before you'll be doing your stuff
              break;//you can use break in the end here to break the rest of the loop, because you no longer need it from what you've specified
          }
      }
 }

avatar image DoTA_KAMIKADzE · May 02, 2015 at 10:56 PM 0
Share

I have completely no idea where you want it to be used, but from what you show I'll consider this - you want it to be used in Update function and your amount and positions of objects within that parent can change, so I'll update my answer with code showing how to do that in a while.

avatar image valtteri_m · May 03, 2015 at 08:44 AM 0
Share

XD I only need the information when it starts the game so I decided to use start, but thanks alot!!

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

21 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

Related Questions

A node in a childnode? 1 Answer

Get object child list with tag 2 Answers

Orderby/Sort a List of Lists based on the .Count 1 Answer

Find gameobject with name and if child of Player 1 Answer

Find children by tag from Player 1 Answer


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