How to make all enemies attack after spawning? Not only 1.
I have done the enemy chase script and the enemy spawner but everytime the enemies spawn in the chase the player but only 1 attacks.
Please help
Here is the chase script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chase : $$anonymous$$onoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) <2000)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2)
{
this.transform.Translate(0, 0, 0.05f);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isWalking",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
Here is the spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : $$anonymous$$onoBehaviour
{
public Transform[] spawnLocations;
public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;
// Use this for initialization
void Start ()
{
Spawn();
}
// Update is called once per frame
void Spawn ()
{
whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
whatToSpawnClone[1] = Instantiate(whatToSpawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
whatToSpawnClone[2] = Instantiate(whatToSpawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
}
}
Looks like you spawn them on the same location and follow the same target. The chase script is on each enemy? Do you see the other 2 not moving or are they invisible?
Answer by Commoble · Apr 04, 2017 at 06:19 PM
This is your problem: First, in your Chase script, you've declared
static Animator anim;
Then, in your Chase script's Start event, you have
anim = GetComponent<Animator>();
A static variable is shared by all members of the class. This means that if you have a bunch of objects that have a Chase script on them, you will only have one anim variable that every instance of Chase is sharing. This is a good tool to use for some things, but this is a very bad tool to use when you have a bunch of objects with a script on them that holds a reference to another component on the object.
When each of these objects initialize, each Chase instance will try to set anim to its own animator, overwriting the animator of the previous instance to initialize, and so when everything is finished initializing, the anim variable will hold the Animator of whichever object was the last to initialize -- and so each of your Chase scripts will have a reference to that last object's animator instead of their own animator.
You can fix this by removing the static
keyword from your anim variable. This will cause each Chase script to have its own variable holding its own animator, instead of sharing one with all of them.
Side note: Did you get this from a tutorial somewhere? I've seen several other people do the exact same thing and I'd like to figure out whoever's teaching people this and tell them to stop teaching people bad code.
So in short word what should I do as I took away the Static??
Your answer
Follow this Question
Related Questions
Making an enemy go back to its spawn after losing target 0 Answers
How to rotate the enemy in 2D top down 0 Answers
Have enemy constantly try to get in front of player and push them back 0 Answers
Have enemy constantly try to get in front of player and push them back 0 Answers
Boss ai help EoW 0 Answers