Search Unity

Instantiated Object does not inherit Animator component!

Discussion in 'Scripting' started by VandalPwoof, Aug 22, 2017.

  1. VandalPwoof

    VandalPwoof

    Joined:
    Feb 17, 2017
    Posts:
    23
    Hi, so I have just updated the prefab of my Shiv object to have an animator component, but for some reason, when I instantiated the Shiv object, the animator component did not get instantiated as well. Please advise.

    I believe this is the offending snippet of the code that needs to be addressed?

    EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemtoEquip.Shiv), playerHand.transform.position, playerHand.transform.rotation);

    Is there a GameObject.GetComponent<Animator> that I can fit into that method somewhere?

    Code (CSharp):
    1.     public void EquipWeapon(Item itemtoEquip)
    2.     {
    3.         if (EquippedWeapon != null)
    4.         {
    5.             characterStats.RemoveStatBonus (EquippedWeapon.GetComponent<IWeapon>().Stats);
    6.             Destroy (playerHand.transform.GetChild (0).gameObject);
    7.         }
    8.            
    9.         EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemtoEquip.Shiv),
    10.             playerHand.transform.position, playerHand.transform.rotation);
    11.         equippedWeapon = EquippedWeapon.GetComponent<IWeapon> ();
    12.         equippedWeapon.Stats = itemtoEquip.Stats;
    13.         EquippedWeapon.transform.SetParent (playerHand.transform);
    14.         characterStats.AddStatBonus (itemtoEquip.Stats);
    15.         Debug.Log (equippedWeapon.Stats[0].GetCalculatedStatValue());
    16.     }
    Error Log:
    MissingComponentException: There is no 'Animator' attached to the "Shiv(Clone)" game object, but a script is trying to access it.
    You probably need to add a Animator to the game object "Shiv(Clone)". Or your script needs to check if the component is attached before using it.
    UnityEngine.Animator.SetTrigger (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:308)
    Shiv.PerformAttack () (at Assets/Scripts/Shiv.cs:17)
    PlayerWeaponController.PerformWeaponAttack () (at Assets/Scripts/PlayerWeaponController.cs:26)
    PlayerWeaponController.Update () (at Assets/Scripts/PlayerWeaponController.cs:19)

    Many thanks.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That should work.

    Are you sure the Shiv object has the Animator on the root object? If it's on a child object, you'll have to GetComponentInChildren.
     
  3. VandalPwoof

    VandalPwoof

    Joined:
    Feb 17, 2017
    Posts:
    23
    It is on the root object of the prefab.
     

    Attached Files:

  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Looks like you need to click apply.
     
  5. VandalPwoof

    VandalPwoof

    Joined:
    Feb 17, 2017
    Posts:
    23
    Thank you, it worked now.