Search Unity

Can't get animation to work. Animator Setfloat error

Discussion in 'Animation' started by Daravil, May 7, 2015.

  1. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    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<Animator> ();
    }

    // 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<Rigidbody>().velocity = movement * speed;





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


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




    }






    }
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    mikedamanskis and tnhn_fb_007 like this.
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    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.
     
    mikedamanskis and djenningsais like this.
  4. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    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.
     
  5. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    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?
     
  7. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    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
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Are you compiling in the Monodevelop editor or letting the Unity editor compile?
     
  9. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    I'm not sure I guess in the monodevelop cause I just save my script and that usually does the trick
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    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?
     
  11. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    Yes exactly.
    I am using unity 5 personal edtion
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    I'm at a loss. Can you try to reproduce this in a new project with as little extra stuff in it as possible?
     
  13. Daravil

    Daravil

    Joined:
    May 7, 2015
    Posts:
    7
    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
     
  14. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Well, since two other people have looked at it, at least you know it's not something really obvious. Good luck!
     
  15. Hasanaj

    Hasanaj

    Joined:
    Jun 22, 2015
    Posts:
    21
    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 :) Hope I helped!!!
     
  16. thomasmahler

    thomasmahler

    Joined:
    Mar 18, 2009
    Posts:
    181
    I just figured this out... holy S***, 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.
     
  17. DeagleDon

    DeagleDon

    Joined:
    Sep 28, 2016
    Posts:
    1
    Use this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Anim_player : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.  
    8.     // Use this for initialization
    9.     void Awake () {
    10.         anim = GetComponent<Animator>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         move = Input.GetAxis("Vertical");
    16. //You will need a Speed paraeter for this
    17.         anim.SetFloat("Speed", move);
    18.     }
    19. }
    20.  
     

    Attached Files:

  18. mikedamanskis

    mikedamanskis

    Joined:
    Jul 23, 2019
    Posts:
    4
    Wow, I was having the same issue, and this fixed it!

     
  19. cejuan08

    cejuan08

    Joined:
    Aug 31, 2022
    Posts:
    1
    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<Rigidbody2D>();
    anim = GetComponent<Animator>();
    }



    // 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);
    }
    }
    }
     
  20. JoaoMegel

    JoaoMegel

    Joined:
    Dec 14, 2023
    Posts:
    1
    it is SetFloat not Setfloat