Can't get animation to work. Animator Setfloat error

I am still super new to unity and coding in general so keep that in mind!

I’m trying to get my walking animation to play as soon as the parameter which i called speed > 0.1
I keep getting the same error and after some googling I still didn’t figure it out.

Assets/Scripts/PlayerMovement.cs(28,26): error CS1061: Type Animator' does not contain a definition for SetFloat’ and no extension method SetFloat' of type Animator’ could be found (are you missing a using directive or an assembly reference?)

Any idea what could be wrong?
Heres my script:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
public float speed;
public float floatDown;
public Boundary boundary;
Animator animator;

[System.Serializable]
public class Boundary{
public float xMin, xMax, zMin, zMax, yMin, yMax;
}
// Use this for initialization.
void Start () {
gameObject.tag = “Player”;
animator = GetComponent ();
}

// Update is called once per frame.
void Update () {

//Basic left, right, up, down, movement.
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis (“Jump”);
animator.SetFloat (“Speed”, moveHorizontal);
Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
GetComponent().velocity = movement * speed;

//Clamping so Bubbleboy doesnt exit screen.
GetComponent().position = new Vector3
(
Mathf.Clamp (GetComponent().position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (GetComponent().position.y, boundary.yMin, boundary.yMax),
Mathf.Clamp (GetComponent().position.z, boundary.zMin, boundary.zMax)
);

//Using relativeForce to simulate floating down.
if (Input.GetKey (KeyCode.S)) {
GetComponent().AddRelativeForce (0, floatDown, 0);
}

}

}

Have Animator component in your object with that script?

And seems your script is right.

2 Likes

Hi,

Before posting code, please read “Using code tags”. It’ll help out your readers.

Do you have another class named “Animator” somewhere in your project?

If not, the line looks correct visually (unless I’m missing something). You might try retyping the line in case there’s a strange character or typo in there that we’re not seeing.

2 Likes

Sorry for the messy wall of code ill fix it when i’m back home!

I do not have another Animator class in any script.
I do have an Animation attached to an ‘enemy’ which I made but that shouldn’t interfere right?

I retyped the code tried using it in different ways by using some youtube tutorials but everytime I end up with the same error.

Yep I did attach the Animator component to the right gameobject

The code you posted compiles just fine. This is why I suspect something else is causing the issue. If you did have another Animator class – for example in the global namespace, versus Unity’s Animator which is in the UnityEngine namespace – the compiler could be finding the wrong one.

What version of Unity are you compiling this in?

I don’t have any other animator class in any part of my project though.
I’m using unitys latest version.
Could it be a monodevelop issue?
I had several mono issues like not being able to type ".

Thanks for the help btw it’s much appreciated

Are you compiling in the Monodevelop editor or letting the Unity editor compile?

I’m not sure I guess in the monodevelop cause I just save my script and that usually does the trick

So you save the script, switch to the Unity editor, and the error appears in Unity’s Console view?

Also, what version of Unity are you using?

Yes exactly.
I am using unity 5 personal edtion

I’m at a loss. Can you try to reproduce this in a new project with as little extra stuff in it as possible?

Haha that’s my exact reaction after a day trying to find out this small error.

I’m out of town till sunday.
I’ll try what you said when I get back thanks for the help!
I’ll quote you to let you know if it solved the issue

Well, since two other people have looked at it, at least you know it’s not something really obvious. Good luck!

Hello Guys. I had the same problem and i managed to fix it. Can you tell me what name has your script? If the name of the script is “Animator” then you have to change it as unity gets confused with this script and thinks it is an animator itself :slight_smile: Hope I helped!!!

I just figured this out… holy shit, I’ve been stuck on this for too long. There seems to be a bug in Unity → Make sure you have your Animator Controller in the same folder as your model, otherwise the params won’t update.

2 Likes

Use this script:

using UnityEngine;
using System.Collections;

public class Anim_player : MonoBehaviour {

    Animator anim;

    // Use this for initialization
    void Awake () {
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        move = Input.GetAxis("Vertical");
//You will need a Speed paraeter for this
        anim.SetFloat("Speed", move);
    }
}

2803897--203611--bandicam 2016-09-28 17-45-28-203.jpg

Wow, I was having the same issue, and this fixed it!

HELP I GET THIS ERROR
error CS1061: ‘Animator’ does not contain a definition for ‘Setfloat’ and no accessible extension method ‘Setfloat’ accepting a first argument of type ‘Animator’ could be found (are you missing a using directive or an assembly reference?)

I HONESTLY DON’T KNOW WHAT IT MEANS, I HAVE EVERYTHING WELL

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

public class AIENEMIGA : MonoBehaviour
{
public Animator anim;

[SerializeField]
Transform player;

[SerializeField]
float rangoAgro; // a cuanta distancia el enemigo ve al jugador
public float velocidadMov;
public bool miraDerecha;
Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
miraDerecha = true;
rb2d = GetComponent();
anim = GetComponent();
}

// Update is called once per frame
void Update()
{
// distancia hasta el jugador
float distJugador = Vector2.Distance(transform.position, player.position);
Debug.Log("Distancia del jugador: " + distJugador);

if (distJugador < rangoAgro && Mathf.abs(distJugador) > 1)
{
PerseguirJugador();
anim.Setfloat(“velocidad”, 1);
anim.SetBool(“atacar”, false);
}
else if (Mathf.abs(distJugador) < 1)
{
anim.SetBool(“atacar”, true);
}
else
{
NoPerseguir();
anim.Setfloat(“velocidad”, 0);
}
}

private void NoPerseguir()
{
rb2d.velocity = Vector2.zero;
}

private void PerseguirJugador()
{
if (transform.position.x < player.position.x)
{
rb2d.velocity = new Vector2 (velocidadMov,0f);
}
else if (transform.position.x > player.position.x)
{
rb2d.velocity = new Vector2 (-velocidadMov,0f);
}
}
}

it is SetFloat not Setfloat