# Any Solution? EnemyAI

**URL:** https://discussions.unity.com/t/any-solution-enemyai/67564
**Category:** Questions & Answers
**Created:** [April 2, 2013, 5:58am UTC](https://discussions.unity.com/t/any-solution-enemyai/67564 "2013-04-02T05:58:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jeremias](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Jeremias](https://discussions.unity.com/u/Jeremias)
#### Post date: [April 2, 2013, 5:58am UTC](https://discussions.unity.com/t/any-solution-enemyai/67564/1 "2013-04-02T05:58:43Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![Jeremias](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Jeremias](https://discussions.unity.com/u/Jeremias)
#### Post date: [April 2, 2013, 8:29am UTC](https://discussions.unity.com/t/any-solution-enemyai/67564/2 "2013-04-02T08:29:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chronos-L](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@Chronos-L](https://discussions.unity.com/u/Chronos-L)
#### Post date: [April 2, 2013, 6:45am UTC](https://discussions.unity.com/t/any-solution-enemyai/67564/3 "2013-04-02T06:45:19Z")

</div>

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.
