Null Reference Exception

I have a Missile Command game in development for one of my classes but I’m getting this error “Null Reference Exception: Object reference not set to an instance of an object”. When I double click on the error in the editor I’m brought to this line “Destination = GetComponent().missileDeathLoc;” which doesn’t really make sense to me. This error happens whenever the player clicks during play. The code below is what I have on the missiles the player fires and the controls code. Any help is appreciated.

using UnityEngine;
using System.Collections;
using System;

public class PlayerMissile : MonoBehaviour
{

    #region Fields
	public float speed;
    private Transform MissileTransform;
    private Vector3 Destination;
    #endregion

    void Start()
    {
        MissileTransform = transform;
        Destination = GetComponent<Controls>().missileDeathLoc;
    }

    //Update is called once per frame
    void Update()
    {
        //Check to see if the missile is at the destination
        if (MissileTransform.position != Destination)
        {
            Alive();
        }
        else if (MissileTransform.position == Destination)
        {
            Dead();
        }
    }

    void Alive()
    {
        //looks at the destination and moves towards it
        MissileTransform.LookAt(Destination);
        MissileTransform.Translate(Vector3.forward * (speed * Time.deltaTime));
    }

    void Dead()
    {
        //deletes the missile if at the destination and subtracts the number of missiles onscreen
        GetComponent<Controls>().missileCount--;
        Destroy(gameObject);
    }
}
using UnityEngine;
using System.Collections;

public class Controls : MonoBehaviour 
{
    public GameObject Missile;
    public GameObject[] Command = new GameObject[3];
    public int missileCount;
    public Vector3 missileDeathLoc;
    private Vector3 missileSpawn;
    private Transform[] CommandTransform = new Transform[3];
    private Ray missileVector;
    private RaycastHit RayHitLoc;

	// Use this for initialization
	void Start () 
    {
        CommandTransform[0] = Command[0].transform;
        CommandTransform[1] = Command[1].transform;
        CommandTransform[2] = Command[2].transform;
        missileCount = 0;
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (missileCount >= 0  missileCount <= 3)
        {
            if (Input.GetButtonDown("Fire"))
            {
                missileCount++;
                RayFire();
                if (missileCount == 1)
                {
                    MissileSpawnPoint(CommandTransform[0]);
                    Instantiate(Missile, missileSpawn, Quaternion.identity);
                }
                if (missileCount == 2)
                {
                    MissileSpawnPoint(CommandTransform[1]);
                    Instantiate(Missile, missileSpawn, Quaternion.identity);
                }
                if (missileCount == 3)
                {
                    MissileSpawnPoint(CommandTransform[2]);
                    Instantiate(Missile, missileSpawn, Quaternion.identity);
                }
            }
        }
		if (Input.GetButton("Pause"))
		{
            PauseMenu PauseScript = GetComponent<PauseMenu>();
			Time.timeScale = 0;
			PauseScript.enabled = true;
		}
	}

    void MissileSpawnPoint(Transform CommandTransform)
    {
        missileSpawn = new Vector3(CommandTransform.position.x, CommandTransform.position.y + (CommandTransform.localScale.y / 2),CommandTransform.position.z);
    }

    public void RayFire()
    {
        missileVector = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(missileVector, out RayHitLoc, Mathf.Infinity))
        {
            missileDeathLoc = RayHitLoc.point;
        }
    }
}

Double check that you have a “Controls” script attached to the same GameObject that your PlayerMissile is attached to.

EDIT: maybe you can try putting it in Awake instead?

The Controls script is currently attached to the camera which correctly spawns the missiles at the correct places. Why would I put the controls on a object that’s going to have a short lifespan such as a missile?

I don’t know, I don’t pretend to understand the intent of your game, but that’s what your code indicates:

Destination = GetComponent().missileDeathLoc;

That says, get the “Controls” component attached to the current GameObject. And you have this in your PlayerMissile’s Start method, therefore it’s looking on your PlayerMissile, not your camera.

How would I get it to access “missileDeathLoc” variable in the script that’s on the camera? I thought “GetComponent” does that job.

P.S. I apologize if I seemed presumptious. xD

Assuming your camera is tagged as “MainCamera”

then it’s just Camera.main.GetComponent…

Thanks a lot! Now to get the missiles to correctly destroy.