Search Unity

[SOLVED] Animator in prefab is not playing an animator controller.

Discussion in 'Animation' started by timurzbs, Aug 5, 2021.

  1. timurzbs

    timurzbs

    Joined:
    Jun 17, 2021
    Posts:
    7
    Dear unity community,
    I have an issue with animator controller in my prefab.
    I have scriptable object with basics stats (int for health , string for name), Enemy script with Instantiate function, TakeDamage() function and slot for a prefab with sprites and animations.
    Everything is working fine but when I try to use TakeDamage() it says Animator is not playing an animator controller.
    My Scriptable object code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New enemy", menuName = "Enemy")]
    6. public class Enemy : ScriptableObject
    7. {
    8.     [SerializeField]
    9.     private string enemyName;
    10.     [SerializeField]
    11.     private string[] phrases;
    12.     [SerializeField]
    13.     private int maxHp;
    14.     private int currentHp;
    15.     public GameObject animPrefab;
    16.     private Animator anim;
    17.  
    18.     void Awake()
    19.     {
    20.         currentHp = maxHp;
    21.         animPrefab.SetActive(true);
    22.  
    23.     }
    24.     public void Spawn(Transform transform)
    25.     {
    26.         Instantiate(animPrefab,transform.position,Quaternion.identity);
    27.         anim = animPrefab.GetComponent<Animator>();
    28.     }
    29.     public void TakeDamage()
    30.     {
    31.         if(anim != null)
    32.         {
    33.         anim.SetTrigger("OnHit");
    34.         }
    35.  
    36.     }
    37.  
    38.  
    39.     void LateUpdate()
    40.     {
    41.         if(anim.isActiveAndEnabled == false)
    42.         {
    43.             anim.enabled = true;
    44.         }
    45.     }
    46. }
    My BattleManager script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class BattleManager : MonoBehaviour
    9. {
    10.     public CinemachineVirtualCamera battleCam;
    11.     public GameObject battleCanvas;
    12.     public GameObject defaultUI;
    13.     public Enemy[] enemies;
    14.     public int currentEnemy;
    15.     public Transform[] teamMateSpawnPoints;
    16.     public Transform[] enemySpawnPoints;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.      
    21.     }
    22.     public void StartBattle()
    23.     {
    24.         SetupBattle();
    25.         battleCam.m_Priority = 100;
    26.         battleCanvas.SetActive(true);
    27.         defaultUI.SetActive(false);
    28.     }
    29.     public void EndBattle()
    30.     {
    31.         battleCam.m_Priority = 0;
    32.         battleCanvas.SetActive(false);
    33.     }
    34.     private void SetupBattle()
    35.     {
    36.         enemies[0].Spawn(enemySpawnPoints[0]);
    37.     }
    38.     private void AttackEnemy()
    39.     {
    40.  
    41.     }
    42.  
    43.     // Update is called once per frame
    44.     void Update()
    45.     {
    46.         if(Input.GetKeyDown(KeyCode.Space))
    47.         {
    48.             enemies[0].TakeDamage();
    49.         }
    50.     }
    I already read about this issue but I really didn't understood how to solve it, please help me.

    Scriptable Object
    EnemyScriptableObject.png

    Animation prefab

    prefab.png
     
    Last edited: Aug 5, 2021
  2. timurzbs

    timurzbs

    Joined:
    Jun 17, 2021
    Posts:
    7
    I was trying to access prefab that in my asset folder, not that prefab that was cloned to the scene.
     
unityunity