How to add GameObjects to lists in a different script?

I have a deck script that you can click to draw a card (right now it only draws the “fireball” card)

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

public class Deck : MonoBehaviour {

	public GameObject fireball;
	public GameObject hand;
	public Hand handScript;

	void OnMouseDown(){
		GameObject clone = (GameObject)Instantiate (fireball) as GameObject;
		hand.handScript.Add (clone);
		}
}

I’d like it to instantiate my fireball prefab, and store it in the list of cards in a player’s hand. The list is in a script attached to the hand GameObject in my scene:

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

public class Hand : MonoBehaviour {
	
	public List<GameObject> handList;

	void Start () {
   		handList = new List<GameObject>();
	}
}

When I click on my deck to draw a card I get the error:

“Type UnityEngine.GameObject' does not contain a definition for handScript’ and no extension method handScript' of type UnityEngine.GameObject’ could be found. Are you missing an assembly reference?”

I’m new to unity and coding in general, so I’m assuming I’m just misunderstanding something simple. Any help would be appreciated!

void OnMouseDown(){
GameObject clone = (GameObject)Instantiate (fireball) as GameObject;
hand.handScript.Add (clone);
}

Should be:

         void OnMouseDown(){
             GameObject clone = (GameObject)Instantiate (fireball) as GameObject;
             handScript.handList.Add (clone);
             }

In the Deck class, you’re declaring a variable hand that has a type GameObject. That means, if you’re trying to do hand.handScript, that means it’s searching for a variable called handScript inside of the GameObject class. This class is defined by Unity and it does not have this variable. Because you want to access a script on the hand gameObject, you need to get that component off of the gameObject. You do so, by saying

hand.GetComponent<Hand>(); (that’s how your Hand script is called). Once you do that, you can get the actual list that you have inside the Hand class and call Add() method on that.

( hand.GetComponent<Hand>().handList.Add(object); )

But there’s another thing as well. Because you have public Hand handScript; as well (and if you have set it in the inspector), you can directly get to the script using that variable. So, instead of writing

hand.handScript (because hand is a GameObject and has no handScript variable), you would just write handScript.handList.Add(object);

Another mistake here, is the fact that you’re calling Add() method, on your script.
handScript is just a script that’s attached to a gameObject, so calling Add() on it will throw an error. What you want to do is to access the list that is inside of the class, by saying handScript.handList and then calling an Add() method on it, because handList is actually a list and it has this method attached to it.

And just one quirk:

(GameObject)Instantiate (fireball) as GameObject;

writing (GameOjbect) in front of the Instantiate method, and writing as GameObject at the end is a thing called casting. It just casts the object returned by Instantiate method of type Object into a type of GameObject. It’s exactly the same thing and you can get away by just writing one of them.

(GameObject)Instantiate (fireball)
Instantiate (fireball) as GameObject;

provides the same results.

As you’re new, I tried to explain in detail, how and why it does not work. If you still have questions about it, comment below and I’ll answer.

@Clofarabine Instead of hand.handScript.Add (clone);,

try this hand.GetComponent<Hand>().handList.Add(clone);