Animations only play on first prefab
I've got multiple prefabs of an enemy, but and they both react to the player, and attack, but only the first prefab enemy changes past the default setting. I have an animator, controller, and avatar applied to all prefabs.
I've tried renaming prefabs, but it does not make a difference. However, if I delete the first prefab, the second will begin to move.
I saw a similar question, but they are using code to instantiate their enemies, and I'm not sure it's the same situation.
Here's my AI code so far. I'm sorry if it's too much, but I'd rather post too much rather than too little.
using UnityEngine; using System.Collections;
public class RobotTestAI : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookdistance;
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
public Transform fpsTarget;
public float aimTimer = 0;
public float PunchSpeed = 0;
Renderer myRender;
public GameObject Laser;
public AudioClip boop; // change this sound later.
public AudioClip RayGun; // change this sound later.
public AudioClip Smack;
static Animator anim;
public GameObject tinyexplosion;
public float LaserDamage = 25;
public float PunchDamage = 15;
public GameObject FireOrigin;
// Use this for initialization
void Start () {
myRender = GetComponent<Renderer>();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update() //required for rigid bodies
{
fpsTargetDistance = Vector3.Distance (fpsTarget.position, transform.position); //how far away are you from the target, and you.
if (fpsTargetDistance < enemyLookdistance) {
myRender.material.color = Color.yellow; //if close enough, colour of enemy changes to yellow.
lookAtPlayer ();
print ("Looking at the player.");
}
if (fpsTargetDistance < attackDistance) {
myRender.material.color = Color.red;
print ("ATTACK!");
rangedCombat ();
if (fpsTargetDistance > 2)
{
anim.SetBool ("isIdle", false);
anim.SetBool("isWalking", true);
this.transform.position += transform.forward * enemyMovementSpeed * Time.deltaTime;
}
else if (fpsTargetDistance < 2)
{
anim.SetBool("isWalking", false);
closeCombat ();
}
} else
{
anim.SetBool ("isIdle", true); //is this the problem area?
anim.SetBool("isWalking", false); //are these SetBool's an issue?
myRender.material.color = Color.blue;
}
}
void lookAtPlayer()
{
Vector3 targetPOS = new Vector3 (fpsTarget.position.x, this.transform.position.y, fpsTarget.position.z);
this.transform.LookAt (targetPOS);
}
void closeCombat ()
{
anim.SetBool ("isIdle", true);
if (PunchSpeed < 1) {
PunchSpeed += Time.deltaTime;
}
else
{
PunchSpeed = 0;
print ("SLAP!");
AudioSource.PlayClipAtPoint (Smack, transform.position, 0.3f);
attack(PunchDamage);
}
}
void attack(float damage)
{
RaycastHit hit;
Ray ray = new Ray(FireOrigin.transform.position+(transform.forward*2), FireOrigin.transform.forward);
if(Physics.Raycast(ray,out hit))
{
//Debug.DrawLine( transform.position, hit.point, Color.red);
Debug.Log(hit.point );
print (hit.collider.gameObject);
Instantiate(tinyexplosion, hit.point, Quaternion.identity);
GameObject target = hit.collider.gameObject;
//Destroy (target);
health HP = target.GetComponent<health>();
if (HP != null)
{
HP.ReceiveDamage (damage);
Debug.Log ("Damage taken: " + damage);
}
}
}
void rangedCombat()
{
if (aimTimer < 2) {
if (fpsTargetDistance > 2) {
print ("AIMING");
AudioSource.PlayClipAtPoint (boop, transform.position, 0.3f);
aimTimer += Time.deltaTime;
}
}
else
{
aimTimer = 0;
anim.SetBool ("isWalking", false);
anim.SetBool ("isShooting", true);
print ("PEW PEW");
AudioSource.PlayClipAtPoint (RayGun, transform.position, 0.3f);
GameObject Laserfire = (GameObject)Instantiate (Laser, transform.position+(transform.forward*2), transform.rotation);
//this part is new, for hurting the player. This is what we are testing.
attack(LaserDamage);
//end of test area
anim.SetBool ("isShooting", false);
anim.SetBool ("isWalking", true);
}
}
}
Doing a little bit of bug testing. When the original enemy is still alive in-game, the nearby units keep responding and attacking. If I kill the first prefab enemy, they all stop responding aside of changing direction to face the player.
I'm applying using the Destroy command to remove prefabs when they die, is this relevant?
Answer by Nudge · Aug 01, 2017 at 05:53 AM
A little late but hopefully it helps you and someone else. I had the same problem and discovered my reference to the animator was static.
Changing the animator from static to private/public fixed it for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111422im_/https://answers.unity.com/themes/thub/images/avi.jpg)