NullReferenceException when passing variable to instantiated object

NullReferenceException is for zKothQ.Target, I’ve got that much figured out. I just can’t for the life of me figure out why it’s not referencing anything. I would think that it wouldn’t instantiate my object if there’s no reference, but the object is being instantiated, it just seems zKothQ stays empty.

	private int Metal;
	public GameObject KothQ;
	public KothQ zKothQ;

	private RaycastHit vHit = new RaycastHit ();
	private Ray vRay;

	void Update () {
		vRay = Camera.main.ScreenPointToRay (Input.mousePosition);
		Metal = 10;

		if(Input.GetKeyUp("q")){
			if(Physics.Raycast (vRay, out vHit, 1000)) {
				if(Metal > 0){
					Metal = Metal - 1;
					zKothQ = Instantiate(KothQ, transform.position, transform.rotation) as KothQ;
					zKothQ.Target = vHit.point;
				}
				else{
				}
			}
		}
     }

The variable KothQ is of type GameObject. So you are instantiating a GameObject not a an object of type KothQ. After Instantiating you are trying to cast the GameObject to an object of type KothQ. It results in a null variable because that cannot be done.

Either reference the actual KothQ component on the GameObject prefab when Instantiating, or search for the component after instantiating using the normal GetComponent.

Note: You need to fix the naming of your variables. At the moment they make it kind of hard to talk about. Usually you start with lowercase letter for variables and uppercase for Types. Also kothQ and zKothQ is very little descriptive of their use. It would be better to name them something like: kothQPrefab and kothQ instead.