Patrick Jeffry
Patrick Jeffry

Reputation: 21

Can't get animation to work. Animator Setfloat error

I am still new to Unity and coding in general so keep that in mind! I have asked this question on the Unity forums as well but people have told me that the code should compile fine and that they are at loss so I'm hoping to find some answers here!

I'm trying to get my walking animation to play as soon as the parameter which i called speed > 0.1

I keep getting this 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?

Here's 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);
        }
    }
}

Upvotes: 2

Views: 2374

Answers (2)

Syntasu
Syntasu

Reputation: 69

Going by you're reference image: https://i.sstatic.net/R0qmX.png

You have errors on 2 scripts. From my experience working with unity, this can cause visual studio/mono develop to cause "fake errors".

The error given is most likely cause because one of the script contains an (non-related) error.

The reason why im thinking this, is because i've have compiled youre code in my unity ide, no errors came poping up. Going by the image you provided and knowing that Unity behaves wonky when you have errors in your ide, this is most likely the case.

Edit:

Oh yeah almost forgot;

If this is the case, you can easily notice it when you are typing in the IDE. The auto-completion(IntelliSense) should be messed up and giving the default methods.

Upvotes: 1

Jeremy Barnes
Jeremy Barnes

Reputation: 677

Make sure you don't have a script somewhere called "Animator". If you've got project script with the same name, Unity will prefer your script over the animator class that is part of Unity.

Upvotes: 1

Related Questions