Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Help with GetComponentsInChildren

Discussion in 'Scripting' started by Molix, Jul 22, 2009.

  1. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    I'm a bit stumped. If I call GetComponentInChildren, I get an object, but if I call GetComponentsInChildren I get nothing, not even the one that is there.
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.   Renderer[] rend = GetComponentsInChildren(typeof(Renderer), true) as Renderer[];
    5.   if( rend == null )
    6.   {
    7.     Debug.Log("Failed to find a renderer in " + gameObject.name );
    8.  
    9.     // I don't believe you
    10.     Renderer rend2 = GetComponentInChildren(typeof(Renderer)) as Renderer;
    11.     if( rend2 != null )
    12.       Debug.Log("Liar. I found one. ");
    13.   }
    14. }
    Liar prints for every object. Wha?!
     
  2. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    I still have no idea why that doesn't work, and would still love to hear if anyone knows.

    The only way I could get it to work was to "manually" go through the children recursively and use their renderer member variable.

    In case anyone cares:
    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.   List<Renderer> rc = GetRenderersInChildren( transform );
    5.   Renderer[] rend = rc.ToArray();
    6. }
    7.  
    8. private List<Renderer> GetRenderersInChildren( Transform t )
    9. {
    10.   List<Renderer> renderers = new List<Renderer>();
    11.   if( t.renderer != null )
    12.     renderers.Add( t.renderer );
    13.   foreach( Transform child in t )
    14.     renderers.AddRange( GetRenderersInChildren( child ) );
    15.   return renderers;
    16. }
    17.  
     
  3. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    check:
    Code (csharp):
    1.  
    2. (GetComponentsInChildren(typeof(Renderer), true)).Length
    3.  
    and tell us what u get.
     
  4. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    If I recall correctly, it was returning values, but they could not be cast to Renderer.
     
  5. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    u answered ur self :)

    I think it is because you r casting Component[] to Renderer[]

    instead get the Component[] returned by GetComponentsInChildren, iterate through it and cast each element (u know is a renderer) to Renderer.
     
  6. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    Why are we passing (typeof(Renderer)) if it is not returning Renderers? When you call the singular version, it works as expected:

    Code (csharp):
    1. Renderer rend = GetComponentInChildren(typeof(Renderer)) as Renderer;
    works, but on the same object:

    Code (csharp):
    1. Renderer rends[] = GetComponentsInChildren(typeof(Renderer)) as Renderer[];
    returns null. Maybe it is not an issue with Unity, but a C# array thing?
     
  7. horsman

    horsman

    Joined:
    Jul 19, 2008
    Posts:
    156
    I believe built in arrays are not reference types, and therefore the 'as' keyword wouldn't work.

    You might be able to make the cast using a C-Style cast, however.
     
  8. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    Code (csharp):
    1.  
    2. Component[] rendererComponents = GetComponentsInChildren(typeof(Renderer)) ;
    3.  
    4. List<Renderer> answer = new List<Renderer>();
    5.  
    6. for(int i = 0; i < rendererComponents.Length; i++)
    7. {
    8.     answer.Add((Renderer)rendererComponents[i]);
    9. }
    10.  
    this is it more or less, didn't compile it just typed it here :)
     
  9. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    Thanks, I believe that will work. Hopefully this will save some people some time in the future.
     
unityunity