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

Resolved How To Get The Component From a GameObject Inside a GameObject[]

Discussion in 'Scripting' started by michaelpynes, Jul 9, 2021.

  1. michaelpynes

    michaelpynes

    Joined:
    Sep 12, 2019
    Posts:
    79
    I'm trying to get the collider from the GameObject in a GameObject array. But it gives me the "Object not set to and instance" error. I know I could make a gameObject and make the indexed GameObjects children of it, but I want to be able to do this without need to assign objects in the inspector.
    Code (CSharp):
    1.  
    2. GameObject[] allCarParts;
    3. //The length is already assigned.
    4. for(int z = 0; z < allCarParts.Length; z++)
    5. {
    6.      Collider cl = allCarParts[z].GetComponent<Collider>();
    7.       if (cl == null)
    8.              allCarParts[z].AddComponent<MeshCollider>();
    9. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Well until you put some GameObjects in there, each entry is going to be null.

    That's how reference datatypes work.

    In fact in the above code, even the array itself is null! It doesn't exist until you new it up, and your comment that length is already set is actually very misleading, unless you deleted some code between the array declaration and the for() loop.

    How did you plan to get the required GameObjects into your
    allCarParts
    array then?

    GameObject.FindGameObjectsWithTag<T>() is one way and of course there are others.
     
  3. michaelpynes

    michaelpynes

    Joined:
    Sep 12, 2019
    Posts:
    79
    Solved it. This is what the entire loop looked like. I just put them in the wrong order.
    Code (CSharp):
    1. allCarParts = new GameObject[fullCarBody.transform.childCount];
    2. public
    3.         for(int z = 0; z < allCarParts.Length; z++)
    4.         {
    5.             Collider cl = allCarParts[z].GetComponent<Collider>();
    6.             if (cl == null)
    7.                 allCarParts[z].AddComponent<MeshCollider>();
    8.  
    9.             allCarParts[z] = fullCarBody.transform.GetChild(z).gameObject;
    10.             if (allCarParts[z].CompareTag("CheckingPart"))
    11.                 checkingParts.Add(allCarParts[z]);
    12.         }
    I understand that I didn't communicate clearly what I was doing. That part's on me. This is the solved loop.
    Code (CSharp):
    1. allCarParts = new GameObject[fullCarBody.transform.childCount];
    2. public
    3.         for(int z = 0; z < allCarParts.Length; z++)
    4.         {
    5.             allCarParts[z] = fullCarBody.transform.GetChild(z).gameObject;
    6. Collider cl = allCarParts[z].GetComponent<Collider>();
    7.             if (cl == null)
    8.                 allCarParts[z].AddComponent<MeshCollider>();
    9.             if (allCarParts[z].CompareTag("CheckingPart"))
    10.                 checkingParts.Add(allCarParts[z]);
    11.         }
     
    Kurt-Dekker likes this.