I am working on a script that responds to the player pressing a button by instantiating a prefab into the scene.
The problem is the animation is not working on the spawned prefab. The conditions of the Animator are being met and switching to them but it's stuck in the first animation it starts in (In this case, the walking animation), but when I drag the prefab out by hand, the animation plays just fine when the conditions are met.
What is up with my Unity code that is causing the instantiated prefab to not work while the prefab itself works fine when dragged in?
Code for the AI
{
public NavMeshAgent NAV;
private Animator Anim;
public float BlockTimer = 3f;
public float AttackRadius = 5f;
public float AllyAttackTime = 2f;
public float Speed = 5f;
public float Health = 10f;
public float AllySpotRange = 10f;
bool EnemyAttackPointCheck, EnemySightCheck;
bool HasAttacked = false;
bool HasBlocked = false;
string[] AllyNumbers;
public Transform EnemyObj;
public Transform AllyEndPoint;
public LayerMask EnemyFaction;
public LayerMask Player;
private void Awake()
{
NAV = GetComponent<NavMeshAgent>();
Anim = GetComponent<Animator>();
}
private void Update()
{
EnemySightCheck = Physics.CheckSphere(transform.position, AllySpotRange, EnemyFaction);
EnemyAttackPointCheck = Physics.CheckSphere(transform.position, AttackRadius, EnemyFaction);
if (!EnemyAttackPointCheck && !EnemySightCheck)
{
AllyMove();
Anim.SetTrigger("IsWalking");
}
if (!EnemyAttackPointCheck && EnemySightCheck)
{
AllySpotted();
Anim.SetTrigger("IsMarching");
}
if (EnemyAttackPointCheck && EnemySightCheck)
{
AllyAttack();
Anim.SetTrigger("IsAttacking");
}
}
void AllyMove()
{
Anim.SetBool("IsWalking", true);
Anim.SetBool("IsMarching", false);
Anim.SetBool("IsAttacking", false);
Anim.SetBool("IsBlocking", false);
NAV.SetDestination(AllyEndPoint.position);
}
void AllySpotted()
{
Anim.SetBool("IsWalking", false);
Anim.SetBool("IsMarching", true);
Anim.SetBool("IsAttacking", false);
Anim.SetBool("IsBlocking", false);
NAV.SetDestination(EnemyObj.position);
Speed = 3f;
}
void AllyAttack()
{
NAV.SetDestination(transform.position);
transform.LookAt(EnemyObj);
if (!HasAttacked)
{
Anim.SetBool("IsWalking", false);
Anim.SetBool("IsMarching", false);
Anim.SetBool("IsAttacking", true);
Anim.SetBool("IsBlocking", false);
HasAttacked = true;
Invoke(nameof(ResetAttackTimer), AllyAttackTime);
}
if (HasAttacked && !HasBlocked)
{
HasBlocked = true;
Anim.SetBool("IsWalking", false);
Anim.SetBool("IsMarching", false);
Anim.SetBool("IsAttacking", false);
Anim.SetBool("IsBlocking", true);
Invoke(nameof(ResetBlockTimer), BlockTimer);
}
}
void ResetAttackTimer()
{
Anim.SetBool("IsAttacking", false);
HasAttacked = false;
}
void ResetBlockTimer()
{
Anim.SetBool("IsBlocking", false);
HasBlocked = false;
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, AttackRadius);
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, AllySpotRange);
}
}
Then the code to instantiate the AI when a button is pressed:
{
private Animator Anim;
public int PlayerGold = 25;
public int PlayerMaxTroops = 15;
private int TroopCounter;
private float GoldCountDownTimer = 10;
private float ResetTimer = 10;
public Transform AllyEndPoint;
public Transform BaseSpawnPoint;
public Transform FootmanObj;
public Text CounterTroop;
public Text CounterGold;
private void Awake()
{
Animator Anim = GetComponent<Animator>();
}
public void BottunPressed()
{
UnitSpawner();
}
private void Update()
{
GoldCountDownTimer -= Time.deltaTime;
if (GoldCountDownTimer < 0 || GoldCountDownTimer == 0)
{
GoldCountDownTimer = ResetTimer;
PlayerGold += 4;
}
CounterTroop.text = TroopCounter.ToString() + "/" + PlayerMaxTroops;
CounterGold.text = PlayerGold.ToString();
}
void UnitSpawner()
{
if (PlayerGold == 5 || PlayerGold > 5 && TroopCounter < PlayerMaxTroops)
{
Instantiate(FootmanObj, BaseSpawnPoint.position, BaseSpawnPoint.rotation);
PlayerGold -= 5;
TroopCounter++;
Debug.Log("Ally spawned " + TroopCounter);
}
else
return;
}
}