Spawned game object does not carry the Animator component
Hi all,
I have an stationary player and a projectile moving towards the player. When a projectile hits the player I have an animation played. At the moment, everything works well for the first projectile but when the spawned projectile hit the player I get the following error:
" MissingComponentException: There is no 'Animator' attached to the "Nuc(Clone)" game object, but a script is trying to access it. You probably need to add a Animator to the game object "Nuc(Clone)". Or your script needs to check if the component is attached before using it. UnityEngine.Animator.SetFloat (System.String name, Single value) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:268) ColisionGreen.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ColisionGreen.cs:37) "
Here is my code:
public class ColisionGreen : MonoBehaviour
{
[SerializeField]
private GameObject GreenParticle;
public float speed;
private Animator playerAnimator;
void Update() {
playerAnimator = GetComponent<Animator> ();
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, new Vector3(0,0,0), step);
transform.Rotate( new Vector3(0, 0, 4), Space.Self );
}
void OnTriggerEnter(Collider other) {
if (other.CompareTag("PlayerGreen")){
Instantiate (GreenParticle, transform.position, Quaternion.identity);
playerAnimator.SetTrigger ("NucAnimSuck");
SpawnNucleosome.totalNuc--;
SpawnNucleosome.dead = true;
Destroy (gameObject);
}
}
}
Any help would be much appreciated.
Cheers