"no Animator attached to the game object" but there is

Hi,

What would be a reason why I get the error:

MissingComponentException: There is no
‘Animator’ attached to the “Character”
game object, but a script is trying to
access it.

… Even though there IS an active animator attached to it?

The problem concerns a line inside of a prefab.

I use

GameObject.Find(“Character”).transform.gameObject.GetComponent().SetTrigger(“StopCharacter”);

The GameObject.Find("Character").transform.gameObject returns the correct gameObject, but for some reason, its Animator cannot be accessed.

EDIT: the target object is not a prefab, but the script that tries to access it is attached to a prefab.

2 Answers

2

Try something like this …

var obj : GameObject;
var _animator : Animator;

function Start ()
{
     obj = GameObject.Find("Character");
     _animator = obj.GetComponent.<Animator>();
     _animator.SetTrigger("StopCharacter");
}

Hi there, thanks for your response. Unfortunately it didn't solve it. Maybe it's a known bug in Unity...

@alex55555 I highly doubt that it’s a bug in Unity. You say that it’s prefab, then how can you find prefab, check that the game object is in sync with the prefab. If still doesn’t work, try making public variable of type Animator, and manually attach it in the inspector. @Griffo you added an extra “.” in the GetComponent

Hi. Actually the target object is not a prefab, but the script that tries to access it is attached to a prefab. Since the target object is not a prefab, it should be accessible with no error.

is the prefab in the scene at the start or do you instantiate it into the scene?

@lordlycastle The exta "." in the GetComponent is Javascript

been there.
please check your scene has only one game object named “Character”.