Error: MissingComponentException: There is no 'Animator' attached - Animator is attached but disappears on play. Can anyone fix?
My script:
using UnityEngine;
using System.Collections;
public class TriggerElevatorDoors : MonoBehaviour
{
public Animator anim;
bool areDoorsOpen = false;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider openDoorCollider)
{
Debug.Log ("Elevator door triggered");
if(!areDoorsOpen)
{
// elevatorDoorsController.SetTrigger ("ElevatorDoorTrigger");
anim.SetBool ("isOpen", true);
areDoorsOpen = true;
}
else
{
anim.SetBool ("isOpen", false);
areDoorsOpen = false;
}
}
}
I have a trigger set to open an elevator door. When triggered I get the error:
MissingComponentException: There is no 'Animator' attached to the "ElevatorDoorTrigger" game object, but a script is trying to access it. You probably need to add a Aniamtor to the game object "ElevatorDoorTrigger". Or your script needs to check if the component is attached before using it.
The problem is that the Animator is definitely assigned but when I click play it disappears. I can't seem to find anyone else having this issue, might be searching for the wrong thing.
Answer by matp86 · Nov 01, 2016 at 09:09 AM
Fixed it. Took out line 16:
// anim = GetComponent<Animator>();
The animator component is part of a different GO.
Answer by Cynikal · Oct 31, 2016 at 08:16 AM
Is this the ONLY script attached to the gameobject?
You can try adding:
[RequireComponent (typeof (Animator))]
public class TriggerElevatorDoors : MonoBehavior
{
...
}
Does ANYTHING else reference the gameobject?
And lastly, are you sure you're not adding the component in play mode...then when you click play (back to edit mode) it reverts?
Answer by matp86 · Oct 31, 2016 at 11:16 AM
@Cynikal Thanks for answering.
In order:
It is the only script attached.
Added that code but no luck.
Nothing else references the gameobject.
I'm not adding anything during play mode.
I'm new to animation and the animator in Unity, I very much appreciate the help!