Any Solution? EnemyAI

NullReferenceException: Object reference not set to an instance of an object
EnemyAI.Update () (at Assets/Scripts/EnemyAI.js:19)

var target : GameObject;
var Weapon : GameObject;   
var lookAtDistance = 20;
var followAtDistance = 15;
var attackAtDistance = 3;
var moveSpeed = 5;
var damping = 6;
var AttackEnabled = 1;
var Speed = moveSpeed;
var Damp = damping;
var cooldown = 2.5;
var distance;

function Start() {
//	target = GameObject.FindGameObjectWithTag("Player");
	target = GameObject.Find("Player");
} 
function Update () {
	distance = Vector3.Distance(target.position, transform.position);
	if(distance < lookAtDistance && Damp > 0) {
		print("EnemyIsLookingYou");
		var rotation = Quaternion.LookRotation(target.position - transform.position);
		transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damp);
	}
	if(distance < followAtDistance && distance > attackAtDistance) {
		print("EnemyIsFollowingYou");
 		transform.Translate(Vector3.forward * Speed *Time.deltaTime);
	}
	else if(distance < attackAtDistance) {
		if(AttackEnabled == 1) {
			AttackEnabled = 0;
			Speed = 0;
			Damp = 0;
			Weapon.animation.Play();
			print("EnemyIsAttacking");
			Cooldown();
		}
	}
}

function Cooldown() {
	while(true) {
		if(AttackEnabled == 0) {
			yield WaitForSeconds(cooldown);
			Speed = moveSpeed;
			Damp = damping;
			AttackEnabled = 1;
		}
		else {
			yield;
		}
	}		
}

Make sure your player is spelled Player and not PLayer or else.

–

On line 17, put the following and see what happens: if (target == null) Debug.Log("Target not found");

–

@fafase i checked, all is spelled all right. @robertbu i added that but nothing happens.

–

have you dragged a gameobject in the "target" and "weapon" field in the editor?

–

2 Answers

2

It Was Working before i added:

target = GameObject.FindGameObjectWithTag("Player");
//or
target = GameObject.Find("Player");

Now even if i try to go back it keeps showing that error.

Change line 19 to:

 distance = Vector3.Distance( target.transform.position, transform.position )

This error occurs when your code Vector3.Distance( target.position, ... ) tries to access the position variable of the target, but target is a gameObject, it does not have position.

To prevent you shooting yourself in the leg in the future, try to put #pragma strict at the first line in your .js scripts.

that works!! but now when i go near the enemy i get this error. NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) EnemyAI.Update () (at Assets/Scripts/EnemyAI.js:22)

–

Thx You Very Much :D

–

Im New at the forum how do i close the topic? or a moderator close this later?

–

Same thing again, change all your target.position to target.transform.position. Or you can declare the target as type Transform: var target : Transform; ... void Start() { //No null handling here, it is assumed that //there is always one player in the scene. target = GameObject.Find("Player").transform; } void Update() { ... }

–

@Chronos-L Thank You For your complete answer.

–