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(“Weapons/” + itemtoEquip.Shiv), playerHand.transform.position, playerHand.transform.rotation);
Is there a GameObject.GetComponent that I can fit into that method somewhere?
public void EquipWeapon(Item itemtoEquip)
{
if (EquippedWeapon != null)
{
characterStats.RemoveStatBonus (EquippedWeapon.GetComponent<IWeapon>().Stats);
Destroy (playerHand.transform.GetChild (0).gameObject);
}
EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemtoEquip.Shiv),
playerHand.transform.position, playerHand.transform.rotation);
equippedWeapon = EquippedWeapon.GetComponent<IWeapon> ();
equippedWeapon.Stats = itemtoEquip.Stats;
EquippedWeapon.transform.SetParent (playerHand.transform);
characterStats.AddStatBonus (itemtoEquip.Stats);
Debug.Log (equippedWeapon.Stats[0].GetCalculatedStatValue());
}
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.
