Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetComponentsInChildren not working with Prefabs?

Discussion in 'Scripting' started by bfogerty, Aug 30, 2011.

  1. bfogerty

    bfogerty

    Joined:
    Nov 12, 2010
    Posts:
    58
    Hi. I have a prefab and one of its children in the hierarchy has a skinned mesh renderer. This is the only skinned mesh renderer in the prefabs hierarchy. I am trying to hide this mesh like so,

    Code (csharp):
    1.  
    2. var renderers : SkinnedMeshRenderer[] = prefabObject.GetComponentsInChildren( SkinnedMeshRenderer ) as SkinnedMeshRenderer[];
    3.  
    4. for(var r : SkinnedMeshRenderer in renderers)
    5. {
    6. r.enabled = false;
    7. }
    8.  
    However renderers keeps returning null.... I don't understand why. I can clearly see the skinnmesh component in the inspector in real time. Any ideas what is wrong? Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nothing to do with prefabs. Actually you can't cast GetComponentsInChildren to an array like that, but you can use generics:

    Code (csharp):
    1. var renderers = prefabObject.GetComponentsInChildren.<SkinnedMeshRenderer>();
    --Eric