How to get all children of a Gameobject with a certain component

I am making a card game and when the cards are dealt they are attached to my Player1 Gameobject (This GO also has other things than cards attached to it).
What I want to do is to find all the cards on the player and add them to an Array/List (The Card Gameobject have a “Card” class attached to them.
This is what I have tried to do:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerGetCards : MonoBehaviour {
    
        int x;
    
        List Children;
    
        TableMaster tMaster;
        PlayerMaster pMaster;
    
        string[] cards;
    
        public List slots;
    
        public void GetCards(bool durchgang)
        {
            pMaster = GetComponent();
            tMaster = pMaster.table.GetComponent();
            StartCoroutine(GetChildren(durchgang));
        }
    
        IEnumerator GetChildren(bool durchgang)
        {
            yield return new WaitForSeconds(1);
            if(!durchgang)
            {
                x = 0;
                foreach(Transform child in this.transform)
                {
                    if(child.GetComponent())
                    {                   
                        AddChildCard(child.name);
                        x++;
                    }
                }
            }
            else
            {
                x = 4;
                foreach(Transform child in this.transform)
                {
                    if (child.GetComponent())
                    {
                        AddChildCard(child.name);
                        x++;
                    }
                }
            }
        }
    
        void AddChildCard(string name)
        {
            cards.SetValue(name, x);
        }
    }
    
`

But for some Reason I get the following error two times:

NullReferenceException: Object reference not set to an instance of an object
PlayerGetCards.AddChildCard (System.String name) (at Assets/Scripts/PlayerScripts/PlayerGetCards.cs:56)
PlayerGetCards+c__Iterator0.MoveNext () (at Assets/Scripts/PlayerScripts/PlayerGetCards.cs:47)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

Thanks in advance!

`

You haven’t initialized your “cards” array anywhere. So it’s default value is “null” (no array). When you try to use the array you get a null reference exception inside your AddChildCard method. You should learn how to read the stacktrace you get in the error. It tells you exactly the line where the error occurred (Line 56).

Also if you want to set the element in an array you usually use:

cards[x] = name;

and not SetValue. Keep in mind that you have to create your array “large enough”. Arrays can’t be resized at runtime

You can short your code a lot use getcompenentsinchildren() to get all cards
Sry Im writing on mobilephone, so the formation is missing.