-1

Here is the code being use:

private void start()
{
    for (int i = 0; i < 5; i++) 
    {
        GameObject card = Instantiate(deck[i], playersHandContent.transform);
        card.GetComponent<Button>().onClick.AddListener(delegate { AddCardToUse(card); });
    }
}

void AddCardToUse(GameObject cardSelected)
{
    Debug.Log(cardSelected.name);
    cardsSelected.Append(cardSelected);
}

I current calling a function when a card is clicked in the Unity UI. The card name is correct so it know its finding the object but when I attempt to append the GameObject to an array the error: "ArgumentNullException: Value cannot be null. Parameter name: source" appears. I am just trying to add the object to the array. Currently the system instantiates the object and adds a listener for the function below to add append it to an array, or atleast thats what I want it to do. I have no idea why im getting this issue as I've never encountered it in unity before.

SO far I have tried diffrent ways to get the GameObject though the button but all result in this out come. One version I tried was using this to get the object: UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject Even this way I still get the same reult

This also produced the same error: card = selectedCard; cardsSelected.Append(card); also give the same result

2
  • 1
    you mean a list???? Commented Jan 11, 2024 at 21:10
  • sounds like cardsSelected is null ... yesterday I saw the same question posted on a different account ... could you stick to that one please? ;) Also add enough details to reproduce your issue .. we don't see what cardsSelected selected even is ... but if it is still an array (as the last time) then I can only repeat @BugFinder 's question: Why is this still not a List if you are going to extend it on runtime? Commented Jan 12, 2024 at 9:47

2 Answers 2

0

It seems like the ArgumentNullException comes from the Enumerable.Append method you are calling: so cardsSelected is null, not cardSelected.

Also, the Enumerable.Append method does something different than what you are expecting it to do. It returns an Enumerable with all the elements of the array and the new element. It does not change the original array. In C#, arrays have a fixed length, so if you want to be able to add and remove items from cardsSelected, a List (from System.Collections.Generic) better suits your needs. Make sure to create the List as well (you can do so at the same place in your code where you declare it), so you do not run into the same error that you have now. Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Changing the object to lists and then declaring them as new game objects list seemed to solve my problem

selectedCards = new List<GameObjects>();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.