Very strange NullReference Problem

Sorry to keep asking questions but this one has me truly stumped.

I managed to get a pursuit AI function working for one of my AI’s, but when I copied the portion of the script over to the other AI objects and changed the GetComponent section to look at the other AI’s script to access it, I keep getting a weird NullReferenceException saying that nothing has been referenced even though all the spelling is correct and the script is attached to the AI.

Here are the code sections

EnemyAI - Working

function Intercept()

{
	var Target : Transform = target.transform;
	var rotation = transform.rotation;
	var position = transform.InverseTransformPoint(Target.position);
	var direction : float = position.x > 0 ? 0.01 : - 0.01;
	var angle = Vector3.Angle (Vector3.forward, position)* direction;
	var targetDirection = (Target.position - transform.position).normalized;
	var lookRotation = Quaternion.LookRotation (targetDirection);
	transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
	transform.Rotate (0,0, -angle * Roll);
	
	
	if (Vector3.Distance(Target.position, transform.position) <= VisualRange)
	{
		transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
		IsIntercepting = false;
		//Debug.Log("Visual Contact! Moving to Attack - Red Fighter");
		IsAttacking = true;
		Attack();
	}
	if (Vector3.Distance(Target.position, transform.position) <= RadarRange || Vector3.Angle(Target.position, transform.position) < 10)
	{
		var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;
		var TargetFuturePosition = Target.position + targetDirection * EnemySpeed;
		var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
		transform.Translate (0, 0, InterceptSpeed * Time.deltaTime);
		var lookFutureRotation = Quaternion.LookRotation (FutureDirection);
		transform.rotation = Quaternion.Slerp(rotation, lookFutureRotation, TurnSpeed * Time.deltaTime);
		transform.Rotate (0,0, -angle * Roll);
		IsIntercepting = true;
		IsPatrolling = false;
		IsEvading = false;
		IsAttacking = false;
	}
	
	if (Vector3.Distance (Target.position, transform.position) > RadarRange)
	{
		IsIntercepting = false;
		//Debug.Log("Radar contact lost, returning to patrol - Red Fighter");
		IsPatrolling = true;
		transform.Translate (0, 0, PatrolSpeed * Time.deltaTime);
		Patrol();
	}
}

Friendly AI - Not working

function Intercept()

{
	var Target : Transform = target.transform;
	var rotation = transform.rotation;
	var Position : Vector3 = transform.InverseTransformPoint(Target.position);
	var Direction : float = Position.x > 0 ? 0.01 : -0.01;
	var Angle = Vector3.Angle (Vector3.forward, Position) * Direction;
	var targetDirection = (Target.position - transform.position).normalized;
	var lookRotation = Quaternion.LookRotation (targetDirection);
	transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
	transform.Rotate (0, 0, -Angle * Roll);
	
	if (Vector3.Distance(Target.position, transform.position) <= VisualRange)
	{
		transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
		IsIntercepting = false;
		//Debug.Log("Visual Contact! Moving to Attack - Blue Fighter");
		IsAttacking = true;
		Attack();
	}
	
	if (Vector3.Distance(Target.position, transform.position) <= RadarRange || Vector3.Angle(Target.position, transform.position) < 10)
	{
		var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;
		var TargetFuturePosition = Target.position + targetDirection * EnemySpeed;
		var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
		transform.Translate (0, 0, InterceptSpeed * Time.deltaTime);
		var lookFutureRotation = Quaternion.LookRotation (FutureDirection);
		transform.rotation = Quaternion.Slerp(rotation, lookFutureRotation, TurnSpeed * Time.deltaTime);
		transform.Rotate (0,0, -Angle * Roll);
		IsIntercepting = true;
		IsPatrolling = false;
		IsEvading = false;
		IsAttacking = false;
	}
	
	if (Vector3.Distance (Target.position, transform.position) > RadarRange)
	{
		IsIntercepting = false;
		//Debug.Log("Radar contact lost, Returning to patrol - Blue Fighter");
		IsPatrolling = true;
		transform.Translate (0, 0, PatrolSpeed * Time.deltaTime);
		Patrol();
	}
}

Now this is where the problem is

Enemy AI - Working

var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;

Friendly AI - Not Working

var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;

Both InterceptSpeed are public variables, yet why does it work for one and not for the other?

You are using

target = GameObject.FindWithTag ("Enemy");

To acquire the EnemyFighter. However, EnemyFighter has a child object also tagged “Enemy”. According to the docs FindWithTag “Returns one active GameObject tagged tag.” It seems you are getting lucky with one script and unlucky with the other! I was able to get things to work nicely by setting the tag of all Children of EnemyFighter to “Untagged”.

So you know the problem is in:

var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;

So either Target is null, or it has no EnemyAI component. That’s the answer. Find out which it is, and fix that.

Debug like this:

var targetAI = Target.GetComponent(EnemyAI);
Debug.Log("targetai="+targetAI,this);

As you say, the scripts are the same. So look elsewhere for the problem. I suspect you’ve dragged the wrong object to ‘Target’.