Cant add gameObject to inventory

Hi! Im working on a card game for a hobby. Im trying to make it so that when I double click on a instantiated card with the help of a raycast that same card is added to an inventory. In this case, that inventory would be the player’s deck. Everything works right up to where the line of code tells unity to add that double clicked card to the deck.

Heres is the AddCardToDeck script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddCardToDeck : MonoBehaviour
{

private float firstClickTime, timeBetweenClicks;
private bool coroutineAllowed;
private int clickCounter;

public PlayerDeck Deck;

public GameObject currentCard;

void Start () 
{
	firstClickTime = 0f;
	timeBetweenClicks = 0.2f;
	clickCounter = 0;
	coroutineAllowed = true;
}

void Update () {

			if (Input.GetMouseButtonUp (0))
				clickCounter += 1;

			if (clickCounter == 1 && coroutineAllowed) {
				firstClickTime = Time.time;
				StartCoroutine (DoubleClickDetection ());
			}				
}

private IEnumerator DoubleClickDetection()
{
	Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	RaycastHit hit;
			
	coroutineAllowed = false;
	while (Time.time < firstClickTime + timeBetweenClicks)
	{
		if (clickCounter == 2)
		{

			if (Physics.Raycast (ray, out hit, 150)) {

				if (hit.collider.tag == "ScrollCard") {

					currentCard = hit.collider.gameObject;

					Debug.Log ("Double Click");

					Debug.Log (currentCard.name);

					Deck.AddCard (currentCard); // <<<<<<<<<< Doesnt work

					break;
				}
			}
		}
		yield return new WaitForEndOfFrame ();
	}
	clickCounter = 0;
	firstClickTime = 0f;
	coroutineAllowed = true;
}

}

and here is the PlayerDeckScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDeck : MonoBehaviour
{

[SerializeField] Transform DeckSlotsParent;
[SerializeField] DeckSlot[] DeckSlots;

private void OnValidate ()
{
		DeckSlots = DeckSlotsParent.GetComponentsInChildren<DeckSlot>();

}

public void AddCard(Card card)
{
	bool cardAdded = false;

	for (int i = 0; i < DeckSlots.Length; i++)
	{ 
		if (DeckSlots  *== null)*
  •   	{*
    

_ DeckSlots .Card = card;_
* Debug.Log (card.name + “was added”);*
* cardAdded = true;*
* break; //stops the loop*
* }*
* }*
* if (!cardAdded)*
* {*
* Debug.Log (“Card not Added”);*
* }*
* }*
* public bool RemoveItem(Card card)*
* {*
* for (int i = 0; i < DeckSlots.Length; i++)*
* {*
* if (card.limit == 1)*
* {*
_ DeckSlots .Card = null;
* return true;
}
}
return false;
}
}
Dont mind the RemoveItem bool for now.
I get 2 errors:
-CS1502: The best overloaded method match for PlayerDeck.AddCard(Card)' has some invalid arguments*_</em> <em>_*-CS1503: Argument #1’ cannot convert UnityEngine.GameObject' expression to type Card’
Here is the Card script if it helps:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = “New Card”, menuName = “Card”)]
public class Card : ScriptableObject
{
public int level;
[Space]
public string cardName;
[Space]
public int energy;
public int courage;
public int strength;
public int intelligence;
public int speed;
[Space]
public string type;
public string effect;
public string rarity;
public int id;
[Space]
public Sprite front;
[Space]
public int limit;
[Space]
public bool isCreature;
}*
Thank you guys for taking the time to read. I welcome any help if you have the time._

Hi Nelson!

Upon double clicking the card, you are simply getting the ‘gameObject’ of that card.
currentCard = hit.collider.gameObject;

Your deck however, expects a ‘Card’ to be passed in.

Read the error message!

"The best overloaded method match for PlayerDeck.AddCard(Card)" - The method we think you're trying to use
"has some invalid arguments" - Has invalid arguments
"Argument #1" - The first one is invalid
"Cannot convert UnityEngine.GameObject' expression to type Card" - You're passing in a GameObject, but it expects a 'Card'

I can see that your ‘Card’ class is a scriptable object. I’m not sure how your gameObjects ‘know’ which card they are. One solution would be to have a ‘CardInstance’ class, which contains a reference to which ‘Card’ it is. Then you could possibly have:

CardInstance cardInstance = currentCard.GetComponent<CardInstance>();
Card card = cardInstance.Card;

Deck.AddCard(card);

I hope this helped!

Another tip: Check out C# naming conventions, and C# Properties. Generally, all fields are named in camelCase and properties are named in PascalCase.

Hi Randomman159!

Lots of thanks for the reply but i’m afraid i’m still new at Unity and have yet to work with instances. Could I trouble you to show me how this “CardInstance” class would look like?

Thank you!