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;
}
}
}