Object Reference not Set to an Instance of an Object in C# (Closed)

I have a script detecting if an object has collided with another. I’ve done some tests by calling other variables between the two scripts (now deleted) and that worked fine, no errors. When tiring to set one variable in the script Stats equal to the called variable from the script RocketDamage I get a NullReferenceExeption.

Stats script (error is on the line “isRocketHit = rocketDamage.rocketHit;”)

using UnityEngine;
using System.Collections;

public class Stats : MonoBehaviour {
	public int playerControl = 1;
	public float hp = 1002f;
	public float armor = 1005f;
	RocketDamage rocketDamage;
	public float rocketOut;
	float isRocketHit;
	

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
		//call RocketDamage script because OnTriggerEnter is only allowed once per script
		rocketDamage = GetComponent<RocketDamage>();
		isRocketHit = rocketDamage.rocketHit;
		
		//take dame in returned that hit is true
		if(isRocketHit == 1){
			hp -= Mathf.Round(100 + (100/(armor+1)));
			armor -= 200;
			//output that damage from rocket has been taken
			rocketOut = 0;
		}
	}
	
	//take damage if hit by bullet
	void OnTriggerEnter(Collider bullet_basic){
		hp -= Mathf.Round(1 + (1000/(armor+1)));
		if(armor > 0){
			armor -= 5;
		}
	}
}

RocketDamage script:

using UnityEngine;
using System.Collections;

public class RocketDamage : MonoBehaviour {
	
	Stats stats;
	public float rocketHit = 0f;

	// Use this for initialization
	void Start () {
		
		//call Stats script
		stats = GetComponent<Stats>();
		
	}
	
	// Update is called once per frame
	void Update () {
		//detect if damage from rocket has been taken
		if(stats.rocketOut == 0){
			rocketHit = 0;
		}
	}
	//detect rocket explosion
	void OnTriggerEnter(Collider explosion_basic){
		rocketHit = 1;
	}
}

Thanks in advance, I been attempting to debug and Google search answers for hours now.

What is your RocketDamage component attached to? It’s not finding your component because it doesn’t know which object to get it from. I’m not sure about your syntax either, may or may not work, but when I get a reference to a script it’s a little different. Try changing

RocketDamage rocketDamage = GetComponent<RocketDamage>;

to

RocketDamage rocketDamage = (RocketDamage)this.GetComponent(typeof(RocketDamage));

Or if the script is attached to something else, just change “this” to whatever GameObject the script is attached to. Same thing for getting the stats script.

Steve