Type Animator does not contain a definition for 'SetInteger' (2D)

Hello, I’m making my first 2D project on unity and I Followed a tutorial to animate my sprite but when I enter my code, the console tells me :
“Assets/Scripts/Animator.cs(20,18): error CS1061: Type Animator' does not contain a definition for SetInteger’ and no extension method SetInteger' of type Animator’ could be found. Are you missing an assembly reference?”.

My code :

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

public class Animator : MonoBehaviour {

    Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void Update()
    {

        if (Input.GetKey(KeyCode.D))
        {
            anim.SetInteger("Direction", 1);
        }

        if (Input.GetKeyUp(KeyCode.D))
        {
            anim.SetInteger("Direction", 0);
        }
    }


}

Thank you for your help.

1 Like

I would re-name the class to something else. Unity already has an “Animator” component. And now you created a script with the same exact name. It’s possible when using GetComponent(), it is not getting the Unity Animator, but instead getting the reference to your own script.

Or, explicitly type in the namespace. Ex:
UnityEngine.Animator animator;
animator = GetComponent<UnityEngine.Animator>()

2 Likes