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

How to get mutiple but the same components on gameobjects ?

Discussion in 'Scripting' started by Tomleung, Mar 29, 2014.

  1. Tomleung

    Tomleung

    Joined:
    Oct 4, 2013
    Posts:
    27
    Here are the codes and my questions below, any kinds of help would be greatly appreciated.

    Code (csharp):
    1.  
    2. private GameObject[] object2;
    3.  
    4. void Update () {
    5.  
    6.           object2 = GameObject.FindGameObjectsWithTag("Enemy");
    7.  
    8.           // Here I want to get all the bounds(renderer.bounds) of  the gameobjects in object2
    9.  
    10.           // if only get one bound of one gameobject : Bounds bound = gameobject.renderer.bounds ;
    11.  
    12.           // How should I do in this situation?
    13. }
    14.  
    Thanks!
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Your question is a little ambiguous since I am unsure as to whether you just want to get the renderer components of the "Enemy" objects themselves, or whether you want to get all the child renderer components as well. So I will take a stab at both of these scenarios for you :)

    Get renderer component from each enemy object:
    Code (csharp):
    1.  
    2. private List<Renderer> enemyRendererComponents;
    3.  
    4. private void Start() {
    5.     enemyRendererComponents = new List<Renderer>();
    6.     foreach (var enemy in GameObject.FindGameObjectsWithTag("Enemy")) {
    7.         var renderer = enemy.renderer;
    8.         if (renderer != null)
    9.             enemyRendererComponents.Add(renderer);
    10.     }
    11. }
    12.  
    13. private void Update() {
    14.     // Do what you want with `enemyRendererComponents`...
    15. }
    16.  
    Get all nested renderer components for each enemy object:
    Code (csharp):
    1.  
    2. private List<Renderer> nestedRendererComponents;
    3.  
    4. private void Start() {
    5.     nestedRendererComponents = new List<Renderer>();
    6.     foreach (var enemy in GameObject.FindGameObjectsWithTag("Enemy"))
    7.         GetRendererComponents(enemy.transform, nestedRendererComponents);
    8. }
    9.  
    10. private void GetRendererComponents(Transform parent, List<Renderer> output) {
    11.     var renderer = parent.renderer;
    12.     if (renderer != null)
    13.         output.Add(renderer);
    14.  
    15.     // Get renderer components from child objects.
    16.     int childCount = parent.childCount;
    17.     for (int i = 0; i < childCount; ++i)
    18.         GetRendererComponents(parent.GetChild(i), output);
    19. }
    20.  
    21. private void Update() {
    22.     // Do what you want with `nestedRendererComponents`...
    23. }
    24.  
    I hope that this helps with your problem, or at least get started with your problem :)
     
  3. Tomleung

    Tomleung

    Joined:
    Oct 4, 2013
    Posts:
    27
    That's similar as I wanted. Thank you for your direction you gave me.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Why bother with a recursive method when GetComponentsInChildren<Renderer>() will do it for you?
     
  5. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I think the OP wanted to get the child components from multiple parent nodes. My approach will only allocate one list whereas the GetComponentsInChildren approach would allocate one list for each parent which would then need to be combined into the final list.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Still...

    Code (csharp):
    1.  
    2. var rList = new List<Renderer>();
    3. foreach (var enemy in enemyList)
    4.     rList.AddRange(enemy.GetComponentsInChildren<Renderer>());
    5.