Search Unity

Question 'AnimationHandler' does not contain a definition for 'SetFloat'

Discussion in 'Animation' started by JoeyMoo, Mar 30, 2022.

  1. JoeyMoo

    JoeyMoo

    Joined:
    Mar 30, 2022
    Posts:
    1
    Following a YouTube tutorial and no one in the comments has a solution for this problem.

    Edit: In Visual Studio the scripts say no issues found but then in Unity I still get this bug. Maybe it's a compatibility issue and I need to fix it somehow? Not sure but if someone could shine some light on this that would be cool.


    At around 25:11 are the two lines that my code doesn't like

    Been trying to fix this bug for the past day but I have literally no idea what's wrong. Here's the error message:

    Assets\AnimatorHandler.cs(73,18): error CS1061: 'AnimatorHandler' does not contain a definition for 'SetFloat' and no accessible extension method 'SetFloat' accepting a first argument of type 'AnimatorHandler' could be found (are you missing a using directive or an assembly reference?)


    At line 73 and 74 in AnimatorHandler.cs I know that but I can't figure out why it's giving me the error when I have watched the video 5 times to figure out what's wrong.

    AnimatorHandler.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace JM
    6. {
    7.     public class AnimatorHandler : MonoBehaviour
    8.     {
    9.         public AnimatorHandler anim;
    10.         int vertical;
    11.         int horizontal;
    12.         public bool canRotate;
    13.  
    14.         public void Initialize()
    15.         {
    16.             anim = GetComponent<AnimatorHandler>();
    17.             vertical = Animator.StringToHash("Vertical");
    18.             horizontal = Animator.StringToHash("Horizontal");
    19.         }
    20.  
    21.         public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
    22.         {
    23.             #region Vertical
    24.             float v = 0;
    25.  
    26.             if (verticalMovement > 0 && verticalMovement < 0.55f)
    27.             {
    28.                 v = 0.5f;
    29.             }
    30.             else if (verticalMovement > 0.55f)
    31.             {
    32.                 v = 1;
    33.             }
    34.             else if (verticalMovement < 0 && verticalMovement > -0.55f)
    35.             {
    36.                 v = -0.5f;
    37.             }
    38.             else if (verticalMovement < -0.55f)
    39.             {
    40.                 v = -1;
    41.             }
    42.             else
    43.             {
    44.                 v = 0;
    45.             }
    46.             #endregion
    47.  
    48.             #region Horizontal
    49.             float h = 0;
    50.  
    51.             if (horizontalMovement > 0 && horizontalMovement < 0.55f)
    52.             {
    53.                 h = 0.5f;
    54.             }
    55.             else if (horizontalMovement > 0.55f)
    56.             {
    57.                 h = 1;
    58.             }
    59.             else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
    60.             {
    61.                 h = -0.5f;
    62.             }
    63.             else if (horizontalMovement < -0.55f)
    64.             {
    65.                 h = -1;
    66.             }
    67.             else
    68.             {
    69.                 h = 0;
    70.             }
    71.             #endregion
    72.  
    73.             anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
    74.             anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
    75.         }
    76.  
    77.         public void CanRotate()
    78.         {
    79.             canRotate = true;
    80.         }
    81.  
    82.         public void StopRotation()
    83.         {
    84.             canRotate = false;
    85.         }
    86.     }
    87. }
    PlayerLocomotion.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace JM
    6. {
    7.     public class PlayerLocomotion : MonoBehaviour
    8.     {
    9.         Transform cameraObject;
    10.         InputHandler inputHandler;
    11.         Vector3 moveDirection;
    12.  
    13.         [HideInInspector]
    14.         public Transform myTransform;
    15.         [HideInInspector]
    16.         public AnimatorHandler animatorHandler;
    17.  
    18.         public new Rigidbody rigidbody;
    19.         public GameObject normalCamera;
    20.  
    21.         [Header("Stats")]
    22.         [SerializeField]
    23.         float movementSpeed = 5;
    24.         [SerializeField]
    25.         float rotationSpeed = 10;
    26.  
    27.         void Start()
    28.         {
    29.             rigidbody = GetComponent<Rigidbody>();
    30.             inputHandler = GetComponent<InputHandler>();
    31.             animatorHandler = GetComponentInChildren<AnimatorHandler>();
    32.             cameraObject = Camera.main.transform;
    33.             myTransform = transform;
    34.             animatorHandler.Initialize();
    35.         }
    36.  
    37.         public void Update()
    38.         {
    39.             float delta = Time.deltaTime;
    40.  
    41.             inputHandler.TickInput(delta);
    42.  
    43.             moveDirection = cameraObject.forward * inputHandler.vertical;
    44.             moveDirection += cameraObject.right * inputHandler.horizontal;
    45.             moveDirection.Normalize();
    46.  
    47.             float speed = movementSpeed;
    48.             moveDirection *= speed;
    49.  
    50.             Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
    51.             rigidbody.velocity = projectedVelocity;
    52.  
    53.             if (animatorHandler.canRotate)
    54.             {
    55.                 HandleRotation(delta);
    56.             }
    57.  
    58.         }
    59.  
    60.         #region Movement
    61.         Vector3 normalVector;
    62.         Vector3 targetPosition;
    63.  
    64.         private void HandleRotation(float delta)
    65.         {
    66.             Vector3 targetDir = Vector3.zero;
    67.             float moveOverride = inputHandler.moveAmount;
    68.  
    69.             targetDir = cameraObject.forward * inputHandler.vertical;
    70.             targetDir += cameraObject.right * inputHandler.horizontal;
    71.  
    72.             targetDir.Normalize();
    73.             targetDir.y = 0;
    74.  
    75.             if (targetDir == Vector3.zero)
    76.                 targetDir = myTransform.forward;
    77.  
    78.             float rs = rotationSpeed;
    79.  
    80.             Quaternion tr = Quaternion.LookRotation(targetDir);
    81.             Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
    82.  
    83.             myTransform.rotation = targetRotation;
    84.         }
    85.  
    86.         #endregion
    87.     }
    88. }
     
    Last edited: Mar 30, 2022
  2. KapitanushkaPVS

    KapitanushkaPVS

    Joined:
    Oct 29, 2021
    Posts:
    1
    I'm a little too lazy to go into details, but since no one answered, then if you change the value not of the animator itself, but of a variable in the script, then you do not need to write SetFloat ("name", 999f). You should write name = 999 instead. example player.GetComponent<ScriptName>().boobsSize = 4.5f That's it.