- Home /
Coroutine to WaitForSeconds() on enemy AI causing all instances of AI to pause. Solution?
I have two zombie prefabs instantiated right now, both with the following script in the prefab.
void Update () {
player = GameObject.FindGameObjectWithTag ("Player");
allies = GameObject.FindGameObjectsWithTag ("EnemyOther");
allyCount = allies.Length;
float distance = Vector3.Distance (transform.position, player.transform.position);
Debug.DrawRay (new Vector3(transform.position.x, transform.position.y + 1, transform.position.z), (player.transform.position - new Vector3(transform.position.x, transform.position.y + 1, transform.position.z)), Color.red);
if (Physics.Raycast (new Vector3 (transform.position.x, transform.position.y + 1, transform.position.z), (player.transform.position - new Vector3(transform.position.x, transform.position.y + 1, transform.position.z)), out hit, maxRange, 9));
{
/*for (int i = 0; i < allyCount; i++)
{
if (Vector3.Distance (allies[i].transform.position, transform.position) < maxRange && distance >= 1.7 && !attacking)
{
bool pursuing = allies[i].GetComponent <ZombieAI>().pursuing;
lastKnownPosition = player.transform.position;
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
transform.position += transform.forward * moveSpeed * Time.deltaTime;
animation.CrossFade("run");
}
}*/
if(hit.transform.tag == "PlayerOther" && distance >= 1.7 && !attacking)
{
lastKnownPosition = player.transform.position;
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
transform.position += transform.forward * moveSpeed * Time.deltaTime;
animation.CrossFade("run");
}
if(hit.transform.tag == "PlayerOther" && distance <= 1.8 && !attacking)
{
attacking = true;
StartCoroutine (AttackAnimation());
}
if (hit.transform.tag != "PlayerOther")
{
transform.LookAt (lastKnownPosition);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
animation.CrossFade ("run");
}
}
}
IEnumerator AttackAnimation()
{
animation.CrossFade ("attack1");
yield return new WaitForSeconds(animation["attack1"].length);
PlayerStats.Health = PlayerStats.Health - 10;
attacking = false;
}
}
When one zombie gets close enough to attack, all zombies stop in their tracks as well rather than only the one who is close enough. This is my first time using a Coroutine, can I get some pointers? Thanks
How about using 'this' keyword for accessing only the current zombie to which this script is attached that you want to pause?
@Harshad$$anonymous$$: That's pretty pointless since Update and the coroutine run in the local scope of the class. You already accessing the variables on this script by default. You only have to use "this" when you have another variable inside the local scope of a method with the same name as a member of the class. In that case the local variable is used first. To access the member variable you have to use "this".
@Bunny83 didn't know that. 'this' has always confused me right from the beginning since I started learning Java (the real Java) and then shifted to c# after started using Unity. And it still keeps on confusing me. Can you provide me some good reads/articles for understanding that?
Answer by Bunny83 · Jul 01, 2014 at 01:44 PM
Is it possible that you have declared "attacking" as static variable? In that case it is shared across all instances since there is only one "attacking" variable, no matter how many instances you create. Variables which should be per instance must not be static.
Since you didn't include the declaration of "attacking" it's just a guess. However if you have strange interaction between different instances that's usually caused by some hidden global state (like static variables).
Wow that was it lol, can't believe I missed that. Thanks XD
Your answer
![](https://koobas.hobune.stream/wayback/20220613152927im_/https://answers.unity.com/themes/thub/images/avi.jpg)