- Home /
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)
ordered how? by what property? You could use GetComponentsInChildren() to get all child transforms, then sort it somehow I guess?
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
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.
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
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 :)
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
}
}
}
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.
XD I only need the information when it starts the game so I decided to use start, but thanks alot!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613194612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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