0
\$\begingroup\$

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;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Any errors or warnings in the console? \$\endgroup\$
    – Philipp
    Jul 12 at 10:11
  • 1
    \$\begingroup\$ Since instantiation is adding clone to the name, do you have other pieces of code that looks for your object by name and thus can't find it? \$\endgroup\$
    – Zibelas
    Jul 12 at 11:50
  • \$\begingroup\$ There is a error on the console but its for another script I have that I have not finished or being used but unity is reading it. Also the instantiation code is the code that takes a prefab. I have a a slot that I drag the prefab into which is the "Public Transform FootmanOBJ" I have changed the transform to "GameObject" but still no change on the animation \$\endgroup\$ Jul 12 at 15:48

1 Answer 1

0
\$\begingroup\$

I've just fixed this issue in my own game. My issue was I had two animator controllers, one on my character model and one on my empty gameobject I used for my collider. Removing one of the animator controllers seemed to fix my issue. I think having the 2 animator controllers was confusing things, so it was able to recognise that it should be playing the first animation repeatedly but when it came to changing animation (for example doing Anim.SetBool) it didn't know which animator to change the bool for.

\$\endgroup\$
1
  • \$\begingroup\$ Remember to click the checkmark icon in the top left of the answer to mark it as "Accepted" if it worked for you. \$\endgroup\$
    – DMGregory
    Jul 30 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .