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! `