0
\$\begingroup\$

I followed some tutorials to add WASD movement to a character; I managed to set up the animator thing and it works in the bottom right corner viewport.

enter image description here

Next I tried adding the following C# script with basic movements:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppleInput : MonoBehaviour
{
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
        animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
    }
}

Configured like so:

enter image description here

But then I get the error message:

MissingComponentException: There is no 'Animator' attached to the "action10Prob_normalsDONEyet" game object, but a script is trying to access it.

You probably need to add a Animator to the game object "action10Prob_normalsDONEyet". Or your script needs to check if the component is attached before using it.

How do I solve this error?

\$\endgroup\$
0
0
\$\begingroup\$

This line:

animator = GetComponent<Animator>();

...says "find an Animator type component attached to the same GameObject that I (AppleInput) am attached to.

But as we can see in your screenshot, you don't have an Animator component on the object "action10Prob_normalsDONEyet". So this line is returning a null (really, a special dummy component that the Unity Editor uses to give you better error messages when testing).

When your Update code tries to use the animator like so:

animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));

Then you get the error message: you tried to call SetFloat on the absence of an Animator.

The fix for this will depend on your setup. If you already have an Animator, but it's somewhere in a child object under this one, you can either:

  • Change GetComponent<Animator> to GetComponentInChildren<Animator> to search child objects too.

  • Or, get rid of this top parent object, and put your AppleInput script on whichever child has an Animator attached directly instead.

  • Or, make your Animator animator variable public so that you can use the Unity Inspector to assign a reference to whichever Animator you want to use.

If you don't have an Animator component anywhere in the object hierarchy, then it's time to add one to the object with the AppleInput script, so it can find it where it's trying to look for it.

Note that this is one of the most common beginner errors in Unity, so just searching "MissingComponentException unity" will turn up tons of existing answers and documentation about it. So please always make that your first step when troubleshooting an error message.

\$\endgroup\$
7
  • \$\begingroup\$ Thanks, I'll keep it in mind when I get errors. So I did the first and 3rd step from your bullet list, seems to be no errors, but I get a grey viewport - imgur.com/a/W6Ndrn8 I did one step, then another, then did both, but no luck. \$\endgroup\$
    – lunaLu
    Jun 7 '20 at 16:20
  • \$\begingroup\$ Those three points were alternatives — pick one or another, not more than one. It's hard to say by the screenshot because the compression makes it illegible, but it looks like you might have accidentally deleted your camera somewhere along the way, or pointed it such that it can't see your apple. \$\endgroup\$
    – DMGregory
    Jun 7 '20 at 16:22
  • \$\begingroup\$ yeah whoops I added a new camera. But still, I can see the character on the camera's preview, but when I press the play button, the viewport is still grey. Should I try the factory reset? \$\endgroup\$
    – lunaLu
    Jun 7 '20 at 16:34
  • \$\begingroup\$ Don't ask me. Save your project to a new folder, try what you have in mind, and if it goes wrong, revert. You never need to ask for an Internet stranger's permission to try fixing your problem independently. \$\endgroup\$
    – DMGregory
    Jun 7 '20 at 16:36
  • \$\begingroup\$ it wasn't a permission, it was a question. I searched the problem about why my viewport is grey, and a lot of the solutions were to do factory reset. But since sometimes, these resets can affect your work in general, I asked if I should do it, or would that mean nothing or just make things worse. Anyways, I did it, but the problem still persists. \$\endgroup\$
    – lunaLu
    Jun 7 '20 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.