# enemy attack AI

**URL:** https://discussions.unity.com/t/enemy-attack-ai/509806
**Category:** Scripting
**Tags:** Scripting
**Created:** [July 16, 2013, 5:21pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806 "2013-07-16T17:21:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ks9109](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@ks9109](https://discussions.unity.com/u/ks9109)
#### Post date: [July 16, 2013, 5:21pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/1 "2013-07-16T17:21:27Z")

</div>

trying to add a script to an enemy but the errors that i have on the script are odd to me its saying expecting } found " but there are no " anywhere on that line of code please someone assist me…idk how to put the code in the box on here please bare with me…

var attack : int = 5;  
var nextAttack : int = 0;

function Update () {

playerDist = Vector3.Distance(transform.position , player.position);

if(playerDist \<= attackDist){

transform.LookAt(player);  
animation.CrossFade (“attack”);

Attack();

return;

}

if(playerDist \<= seekplayerDist){

transform.LookAt(player);  
transform.position += transform.forward \* speed \* Time.deltaTime;  
animation.CrossFade (“run”);

} else {

animation.CrossFade (“idle”);

}

}

function Attack (){

var attackRate : int = 5;  
var nextAttack : int = 0;

attackStrenght = weaponDamage + strenght;  
damage = (opponentAC) - (attackStrenght);  
weaponDamage = Random.Range(1.0, 6.0);

if (Time.time \> nextAttack) {

nextAttack = Time.time + attackRate;  
[//playerStats.health](https://playerStats.health) += damage;  
}

---

<div class="post-metadata">

### Author: ![Eiznek](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eiznek](https://discussions.unity.com/u/Eiznek)
#### Post date: [July 16, 2013, 6:12pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/2 "2013-07-16T18:12:46Z")

</div>

Use Code tags…

I only found that you missed a } at the end.

Maybe seperate your { } on different lines if you have problems… I use this as habit now a days. Its easier to see what bracket matches the other.

Example:

```auto
public class MyClass
{
	public void SomeMethod()
	{
		if(IsSomethingHappening())
		{
			//Do something
		}
	}

	private bool IsSomethingHappening()
	{
		return true;
	}
}

```

```auto
var attack : int = 5;
var nextAttack : int = 0;

function Update () {
	playerDist = Vector3.Distance(transform.position , player.position);

	if(playerDist <= attackDist) {
		transform.LookAt(player);
		animation.CrossFade ("attack");
		Attack();
		return;
	}

	if(playerDist <= seekplayerDist) {
		transform.LookAt(player);
		transform.position += transform.forward * speed * Time.deltaTime;
		animation.CrossFade ("run");
	} else {
		animation.CrossFade ("idle");
	}
}

function Attack () {
	var attackRate : int = 5;
	var nextAttack : int = 0;

	attackStrength = weaponDamage + attackStrength;
	damage = (opponentAC) - (attackStrength);
	weaponDamage = Random.Range(1.0f, 6.0f);

	if (Time.time > nextAttack) {

		nextAttack = Time.time + attackRate;
		//playerStats.health += damage;
	}
} // <--- You missed this bracket.

```

---

<div class="post-metadata">

### Author: ![Ezro](https://avatars.discourse-cdn.com/v4/letter/e/65b543/32.png) [@Ezro](https://discussions.unity.com/u/Ezro)
#### Post date: [July 16, 2013, 6:15pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/3 "2013-07-16T18:15:11Z")

</div>

> [@ks9109](#):
>
> trying to add a script to an enemy but the errors that i have on the script are odd to me its saying expecting } found " but there are no " anywhere on that line of code please someone assist me…idk how to put the code in the box on here please bare with me…
> 
> var attack : int = 5;  
> var nextAttack : int = 0;
> 
> function Update () {
> 
> playerDist = Vector3.Distance(transform.position , player.position);
> 
> if(playerDist \<= attackDist){
> 
> transform.LookAt(player);  
> animation.CrossFade (“attack”);
> 
> Attack();
> 
> return;
> 
> }
> 
> if(playerDist \<= seekplayerDist){
> 
> transform.LookAt(player);  
> transform.position += transform.forward \* speed \* Time.deltaTime;  
> animation.CrossFade (“run”);
> 
> } else {
> 
> animation.CrossFade (“idle”);
> 
> }
> 
> }
> 
> function Attack (){
> 
> var attackRate : int = 5;  
> var nextAttack : int = 0;
> 
> attackStrenght = weaponDamage + strenght;  
> damage = (opponentAC) - (attackStrenght);  
> weaponDamage = Random.Range(1.0, 6.0);
> 
> if (Time.time \> nextAttack) {
> 
> nextAttack = Time.time + attackRate;  
> [//playerStats.health](https://playerStats.health) += damage;  
> }

Hi ks9109,

You are missing a } at the end of your script.

The last } is closing the if statement, and Attack() is left open.

Regards,  
Ezro

---

<div class="post-metadata">

### Author: ![ks9109](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@ks9109](https://discussions.unity.com/u/ks9109)
#### Post date: [July 16, 2013, 7:32pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/4 "2013-07-16T19:32:51Z")

</div>

thank you for the help as soon as i added a bracket it worked but then there were many more errors that were not seen by my naked eye

Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(8,52): BCE0005: Unknown identifier: ‘player’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(10,18): BCE0005: Unknown identifier: ‘attackDist’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(14,18): BCE0005: Unknown identifier: ‘player’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(24,18): BCE0005: Unknown identifier: ‘seekplayerDist’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(26,18): BCE0005: Unknown identifier: ‘player’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(27,43): BCE0005: Unknown identifier: ‘speed’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,18): BCE0005: Unknown identifier: ‘weaponDamage’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,33): BCE0005: Unknown identifier: ‘strenght’.  
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(50,11): BCE0005: Unknown identifier: ‘opponentAC’.

any help…

---

<div class="post-metadata">

### Author: ![Ezro](https://avatars.discourse-cdn.com/v4/letter/e/65b543/32.png) [@Ezro](https://discussions.unity.com/u/Ezro)
#### Post date: [July 16, 2013, 7:53pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/5 "2013-07-16T19:53:38Z")

</div>

> [@ks9109](#):
>
> thank you for the help as soon as i added a bracket it worked but then there were many more errors that were not seen by my naked eye
> 
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(8,52): BCE0005: Unknown identifier: ‘player’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(10,18): BCE0005: Unknown identifier: ‘attackDist’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(14,18): BCE0005: Unknown identifier: ‘player’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(24,18): BCE0005: Unknown identifier: ‘seekplayerDist’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(26,18): BCE0005: Unknown identifier: ‘player’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(27,43): BCE0005: Unknown identifier: ‘speed’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,18): BCE0005: Unknown identifier: ‘weaponDamage’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,33): BCE0005: Unknown identifier: ‘strenght’.  
> Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(50,11): BCE0005: Unknown identifier: ‘opponentAC’.
> 
> any help…

ks9109,

It’s hard to provide help without seeing the full code, but those errors are because you don’t have the variables defined within their relative scope.

Regards,  
Ezro

---

<div class="post-metadata">

### Author: ![ks9109](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@ks9109](https://discussions.unity.com/u/ks9109)
#### Post date: [July 17, 2013, 9:03pm UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/6 "2013-07-17T21:03:44Z")

</div>

when you say that you mean like

var player  
var attackDist  
etc…  
right Ezro

---

<div class="post-metadata">

### Author: ![ks9109](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@ks9109](https://discussions.unity.com/u/ks9109)
#### Post date: [July 20, 2013, 6:24am UTC](https://discussions.unity.com/t/enemy-attack-ai/509806/7 "2013-07-20T06:24:43Z")

</div>

[CODE

]var attack : int = 5;  
var nextAttack : int = 0;  
var attackDist;  
var weaponDamage;  
var strength;  
var opponentAC;  
var Speed;  
var seekplayerDist;

function Update () {

playerDist = Vector3.Distance(transform.position , player.position);

if(playerDist \<= attackDist){

transform.LookAt(player);  
animation.CrossFade (“attack”);

Attack();

return;

}

if(playerDist \<= seekplayerDist){

transform.LookAt(player);  
transform.position += transform.forward \* speed \* Time.deltaTime;  
animation.CrossFade (“run”);

} else {

animation.CrossFade (“idle”);

}

}

function Attack (){

var attackRate : int = 5;  
var nextAttack : int = 0;

attackStrenght = weaponDamage + strenght;  
damage = (opponentAC) - (attackStrenght);  
weaponDamage = Random.Range(1.0, 6.0);

if (Time.time \> nextAttack) {

nextAttack = Time.time + attackRate;  
[//playerStats.health](https://playerStats.health) += damage;  
}  
[/CODE]
