# problema con script de IA

**URL:** https://discussions.unity.com/t/problema-con-script-de-ia/659083
**Category:** C# & .NET
**Tags:** Scripting, Mono
**Created:** [March 22, 2017, 12:30am UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083 "2017-03-22T00:30:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Cincoj](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cincoj/32/227726_2.png) [@Cincoj](https://discussions.unity.com/u/Cincoj)
#### Post date: [March 22, 2017, 12:30am UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083/1 "2017-03-22T00:30:27Z")

</div>

hola amigos, sucede que… u.u programé una inteligencia artificial. consta de 3 scripts, el de la inteligencia artificial que es el que contiene los estados del enemigo, otro de rango de visión y otro del rango de ataque. En el de visión y ataque, definí que cuando el player hiciera colision con ese rango, pasará al estado de persecución y cuando salga, al de reposo, y lo mismo con el de ataque. Ahora, en la IA también tengo el estado de morir, que es lo que pasa con eso? Que la IA funciona, pero cuando coloco varios personajes y mato 1 mueren todos, porque la variable de estados la tengo como estática, osea que al sufrir una modificación allí, se modificara en los demás scripts.

quisiera saber como crear un método para que los enemigos puedan morir por separados

(in english, in this time of google translate u.u )

I programmed an artificial intelligence. Consists of 3 scripts, that of the artificial intelligence that is the one that contains the states of the enemy, another one of range of vision and another one of the range of attack. In the vision and attack, I defined that when the player collided with that range, it will pass to the state of persecution and when it leaves, to rest, and the same with the attack. Now, in AI I also have the state of dying, what’s wrong with that? That AI works, but when I put several characters and kill 1 all die, because the state variable I have as public, so that when undergoing a modification there, it will be modified in the other scripts.

aquí están los codigos (here is the codes):

```javascript
#pragma strict
static var estado : int = 0; //estado de inteligencia artificial en el que se encuentra
var vida : float = 100;

var morir : boolean = false;

var nada : AnimationClip;
var corre : AnimationClip;
var ataca1 : AnimationClip;
var muere : AnimationClip;

var player : Transform;

function Start () {
  
}

function Update () {

if (estado == 0){

GetComponent.<Animation>().Play (nada.name);

}

if (estado == 1){

GetComponent.<Animation>().Play (corre.name);
transform.LookAt(player);
transform.Translate (Vector3 (0,0,1)*Time.deltaTime);

}

if (estado == 2){

GetComponent.<Animation>().Play (ataca1.name);

}

if (estado == 3){

GetComponent.<Animation>().Play (muere.name);

Destroy (this.gameObject, 2);
}

if (vida <= 0){
  
muerto = true;

}

if (muerto == true){

estado = 3;

vida = 0;

}

  }

function OnTriggerEnter (other : Collider ){

if (other.gameObject.tag == "ManodelPlayer"){

vida -= 20;
}

}

```

```javascript
#pragma strict

var on : GameObject;
var off : GameObject;

function Start () {
  
}

function Update () {

}

function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "Player" ){

IA.estado = 1;

}
}

function OnTriggerExit (other : Collider){

if (other.gameObject.tag == "Player" ){

IA.estado = 0;

}

}

```

```javascript
#pragma strict

function Start () {
  
}

function Update () {
  
}

function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "Player" ){

IA.estado = 2;

}
}

function OnTriggerExit (other : Collider){

if (other.gameObject.tag == "Player" ){

IA.estado = 1;

}

}

```

gracias amigos.

thank you guys

---

<div class="post-metadata">

### Author: ![makeshiftwings](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/makeshiftwings/32/334244_2.png) [@makeshiftwings](https://discussions.unity.com/u/makeshiftwings)
#### Post date: [March 22, 2017, 12:54am UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083/2 "2017-03-22T00:54:29Z")

</div>

Sorry, my Spanish is bad. Don’t use “static”. “Static” means it is shared across all the objects. Change it to:

```auto
var estado : int = 0; //no es static!!!

```

To get the matching one, use GetComponent:

```auto
function OnTriggerEnter (other : Collider){
  if (other.gameObject.tag == "Player" ){
    GetComponent.<IA>().estado = 1;

```

Everywhere you have IA.estado change to GetComponent.().estado. Or if it is on a specific enemy like “other”, you can do other.GetComponent.().estado.

---

<div class="post-metadata">

### Author: ![Cincoj](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cincoj/32/227726_2.png) [@Cincoj](https://discussions.unity.com/u/Cincoj)
#### Post date: [March 22, 2017, 3:05am UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083/3 "2017-03-22T03:05:35Z")

</div>

> [@makeshiftwings](#):
>
> Sorry, my Spanish is bad. Don’t use “static”. “Static” means it is shared across all the objects. Change it to:
> 
> ```auto
> var estado : int = 0; //no es static!!!
> 
> ```
> 
> To get the matching one, use GetComponent:
> 
> ```auto
> function OnTriggerEnter (other : Collider){
> if (other.gameObject.tag == "Player" ){
> GetComponent.<IA>().estado = 1;
> 
> ```
> 
> Everywhere you have IA.estado change to GetComponent.().estado. Or if it is on a specific enemy like “other”, you can do other.GetComponent.().estado.

i tryed, but appears this error now:

NullReferenceException: Object Reference not set a instance of an object

i changed the static var to var only, and put the line whit the GetComponent

what can be?

sorry for my english xdd

---

<div class="post-metadata">

### Author: ![makeshiftwings](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/makeshiftwings/32/334244_2.png) [@makeshiftwings](https://discussions.unity.com/u/makeshiftwings)
#### Post date: [March 22, 2017, 4:36pm UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083/4 "2017-03-22T16:36:08Z")

</div>

Is the “IA” script attached to the same GameObject as the enemy Collider?

---

<div class="post-metadata">

### Author: ![Cincoj](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cincoj/32/227726_2.png) [@Cincoj](https://discussions.unity.com/u/Cincoj)
#### Post date: [March 23, 2017, 11:56pm UTC](https://discussions.unity.com/t/problema-con-script-de-ia/659083/5 "2017-03-23T23:56:39Z")

</div>

![3005228--224104--upload_2017-3-23_19-42-47.png](https://europe1.discourse-cdn.com/unity/original/4X/a/e/0/ae02a13560642de7bac8ab3ed5ce804b2cc6ae28.png)

yep, the script is attached to the collider of the enemy collider, and now, don`t know where i have what put this the script. In the vision range or the attack range :v but idk…
