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.
  2. Dismiss Notice

Question How to get List of Rigidbody from List of GameObjects?

Discussion in 'Scripting' started by davidnibi, Dec 18, 2022.

  1. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    I can't figure out the logic of why this isn't working?
    I can get this working with a fixed array, but this same isn't working for lists.

    I've generated my list of GameObjects:

    Code (CSharp):
    1.         for (int i = 0; i < poolAmount; i++)
    2.         {
    3.             int randomIndex = Random.Range(0, objects.Length);
    4.             GameObject obj = (GameObject)Instantiate(objects[randomIndex]);
    5.             obj.SetActive(false);
    6.             pool.Add(obj);
    7.         }
    8.  
    I've tried a few methods in different orders, and I thought the GetComponent way would work, but I'm obviously missing something here:

    Code (CSharp):
    1.      
    2.  
    3.         for (int i = 0; i < poolAmount; i++)
    4.         {
    5.            rb.Add(pool[i].GetComponent<Rigidbody>());
    6.  
    7. or
    8.             rb.Add((Rigidbody)pool[i].GetComponent<Rigidbody>());
    9.  
    10. or
    11.  
    12.           Rigidbody rbobj = pool[i].GetComponent<Rigidbody>();
    13.           rb.Add(rbobj);
    14.  
    15.         }
    16.  
    17.  
    And a foreach after the pool objects are populated:

    Code (CSharp):
    1.         foreach (GameObject poolObjects in pool)
    2.         {
    3.  
    4.             if (poolObjects.gameObject.activeSelf)
    5.             {
    6.                 rb2 = poolObjects.GetComponent<Rigidbody>();
    7.                 rb.Add(rb2);
    8.             }
    9.         }
    The first method populating the list with the correct amount of entries, and foreach does it as the pool is made active, but they're all null in both cases.

    This lists the correct rigidbody in an Update statement:

    Code (CSharp):
    1.         for (int i = 0; i < pool.Count; i++)
    2.         {
    3.             Rigidbody rbrb = pool[i].GetComponent<Rigidbody>();
    4.             Debug.Log(rbrb);
    5.         }
    It should just be a case of getting any component on the gameobject - I know I'm missed something stupid, but I'm stumped.
     
    Last edited: Dec 18, 2022
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    OK ignore me, I was setting

    Code (CSharp):
    1.             obj.SetActive(false);
    And it wasn't finding a Rigidbody on it. I just ran a seperate routine and SetActive in that. ;)

    Code (CSharp):
    1.         for (int i = 0; i < pool.Count; i++)
    2.         {
    3.             Rigidbody rbrb = pool[i].GetComponent<Rigidbody>();
    4.             //Debug.Log(rbrb);
    5.             rb.Add(rbrb);
    6.             pool[i].SetActive(false);
    7.         }