Search Unity

transform.childCount

Discussion in 'Scripting' started by thomasvdb, Jul 21, 2008.

  1. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    I have a GameObject with 4 children. Everytime the user hits the mousebutton the active child shoots away and the next child should become active.

    Now I remembered a code-fragment from the FPS tutorial

    Code (csharp):
    1.  
    2.     void SelectChilds( int index ) {
    3.         for ( int i=0; i < transform.childCount; i++ )  {
    4.             if ( i == index ) {
    5.                 transform.GetChild(i).gameObject.SetActiveRecursively( true );
    6.             } else {
    7.                 transform.GetChild(i).gameObject.SetActiveRecursively( false );
    8.             }
    9.         }
    10.     }
    11.  
    Now my question is: Can I influence the way Unity arranges the children?
    Because now it starts with the 3th children, then the 4th, then the 1st...

    I already used this script for changing weapons and hadn't any problems with it.
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Maybe you should make an array of 4 Transforms and assign them yourself.
     
  3. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    I know but it's nice when a script is reusable... if I need the same script in another project but with 20 children for instance... :p

    But for now this solution works :)
     
  4. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    Hoi,

    You can grab the list of children from unity when your script starts up. store that list. rearrange it to your likings. And then from then on use your own list, instead of asking unity for it.

    Doei, Lucas
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You don't need separate scripts for that. You can just change the length of the array in the inspector.

    Rune
     
  6. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    I know but I still need to assign the 20 children...
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    But only once on the prefab
     
  8. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    Correct :)
    Well, I'll make an array :p just making it too difficult I guess...
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    not really.
    But you seem to be the programmer type like me, not the designer type.

    I still find myself trying to do anything and all in code instead of just using the editor because "2 drag and drops are soooo much more evil than 20 lines of code". Have a tendency to waste time on finding complicated pure source solutions instead of just using the simplicity of the editor, but it is getting better.

    Years of training to do so I guess but the longer I use unity the more I love the fact that I can get behavior to work much faster through prefabs and setting it in the editor.
     
  10. thomasvdb

    thomasvdb

    Joined:
    Feb 28, 2008
    Posts:
    85
    Correct again, I'm a programmer type :)

    I've just graduated in ICT (Information and Communication Technologies) where I've seen multiple programming languages. In those 3 years we've learned to program in such a way we could reuse our code as much as possible.

    In Unity I'm trying to do the same but indeed (like you said) I should use the simplicity of the Unity-editor instead. :)