I am trying to add animations to an enemy but can't seem to get it in the Code

This is from a tutorial that I am working on and I followed it as closely as possible and I just have no idea what’s going wrong since everything else aside from one thing seems to be working fine (I am on Unity 2020.3.4f1) The error I keep getting is “The name animator does not exist in the current context” any help or idea of what’s going wrong and how to fix it?

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

public class EnemyController : MonoBehaviour
{
    public float speed;
    public bool vertical;
    public float changeTime = 3.0f;
    Rigidbody2D rigidbody2D;

    float timer;
    int direction = 1;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        timer = changeTime;
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame

    void Update()
    {
        timer -= Time.deltaTime;


        if (timer < 0)
        {

            direction = -direction;
            timer = changeTime;

        }
    }





    void FixedUpdate()
    {
        Vector2 position = rigidbody2D.position;
       
        if(vertical)
        {
            animator.SetFloat("Move X", 0);
            animator.SetFloat("Move Y", direction);
            position.y = position.y + Time.deltaTime * speed * direction; ;

        }
        else
        {
            animator.SetFloat("Move X", direction);
            animator.SetFloat("Move Y", 0);
            position.x = position.x + Time.deltaTime * speed * direction; ;

        }
      

        rigidbody2D.MovePosition(position);
    }
   
        void OnCollisionEnter2D(Collision2D other)
    {
        RubyController player = other.gameObject.GetComponent<RubyController>();
        if (player != null)
        {
            player.ChangeHealth(-1);

        }

    }



}

Can you please ask Scripting questions on the scripting forum. I appreciate you’re trying to learn C# and probably focusing on 2D but this isn’t what the 2D forum is for. We have dedicated forums for scripting, audio, animation, UI, physics etc.

We also have a Getting Started forum for when you’re starting-up and are struggling with some basic things.

I’ll move your post to the scripting forum.

As to your problem, can you see anywhere where you define your “animator” field? No? That’s why it’s giving you an error, it has no idea what “animator” is you’re referring to therefore it doesn’t even know it’s supposed to be a field nevermind what type it is. :slight_smile:

So just like you defined the other fields, you need to define this one.

If this is a tutorial then you need to perhaps step away and come back and check what you’ve missed again. The point of a tutorial is not to give you free code but to teach so I would suggest moving to another tutorial first that covers basic C# as it’ll put you in a stronger position to follow tutorials on other subjects.

How do I define a field?

There’s a scripting forum AND a Getting Started forum? I’ll be sure to keep both of those in mind for future reference, but in that case, what would go in the 2D forum so I can post the right things in the right places?

When you’re asking something specific about 2D including 2D features. What we want to avoid is the 2D forum being used for anything simply because you’re creating something 2D. We’ll have a noisy 2D forum where we have questions about audio, general graphics, UI etc. All those features have their own dedicated forums. You sound surprised by this so have you even seen the main page of the forum which lists them all?

https://forum.unity.com/

You must know this surely? You’ve got a whole bunch of them defined in your class already. Not that it matters, you should be able to compare your code to the tutorial you’re following?

These are fields.

    public float speed;
    public bool vertical;
    public float changeTime = 3.0f;
    Rigidbody2D rigidbody2D;
    float timer;
    int direction = 1;

So you need this assuming this is what’s in the tutorial.

Animator animator;

I see, this was left out and it completely threw me off, and I didn’t know those were called fields, thank you for everything

What’s the tutorial btw? If it’s in Unity Learn then it can be reported.

The tutorial is called “Ruby’s Adventure: 2D Beginner” the information lacking should be in sprite animation step 9

Nvm I’m just blind, forget I said anything, I must’ve not noticed since it was at the very top

1 Like

Np, good luck.