Search Unity

error CS1061

Discussion in 'Scripting' started by kingdom216, Sep 4, 2021.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    I'm getting the error CS1061: 'PlayerInput.CharacterControlsActions' does not contain a definition for 'Jump' and no accessible extension method 'Jump' accepting a first argument of type 'PlayerInput.CharacterControlsActions' could be found (are you missing a using directive or an assembly reference?)

    I have set up my character control action for the Jump command, does anybody have an idea why this error is coming up. Before anybody suggests for me to google it or look for it on YouTube, I have done that and the tutorial I am following is iHeartGameDev on YouTube and he is a pretty good tutor. However, he is unable to help me with this so I have turned here.
    The walk and run functions work fine, it is only the Jump that gives me an error. Thank you for any help you can offer.
    PlayerInput.PNG
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    7,961
    Post your whole script and whole error message. Also make sure, you are using the proper InputActionAsset and you do not have a duplicate one.
     
    Kurt-Dekker likes this.
  3. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    I'll post the code, but I don't think the error is in the code, but thank you for taking a look at it, I might have made a mistake somewhere. Also I did post the whole error message above.
    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class AnimationMotionController : MonoBehaviour
    8. {
    9.     PlayerInput playerInput;
    10.     CharacterController characterController;
    11.     Animator animator;
    12.  
    13.     int isWalkingHash;
    14.     int isRunningHash;
    15.  
    16.     Vector2 currentMovementInput;
    17.     Vector3 currentMovement;
    18.     Vector3 currentRunMovement;
    19.     bool isMovementPressed;
    20.     bool isRunPressed;
    21.  
    22.     float rotationFactorPerFrame = 1.0f;
    23.     float runMultiplier = 3.0f;
    24.  
    25.     float gravity = -9.8f;
    26.     float groundedGravity = -.05f;
    27.  
    28.     bool isJumpPressed = false;
    29.     float initialJumpVelocity;
    30.     float maxJumpHeight = 1.0f;
    31.     float maxJumpTime = 0.5f;
    32.     bool isJumping = false;
    33.  
    34.     void Awake()
    35.     {
    36.         playerInput = new PlayerInput();
    37.         characterController = GetComponent<CharacterController>();
    38.         animator = GetComponent<Animator>();
    39.  
    40.         isWalkingHash = Animator.StringToHash("isWalking");
    41.         isRunningHash = Animator.StringToHash("isRunning");
    42.  
    43.         playerInput.CharacterControls.Move.started += onMovementInput;
    44.         playerInput.CharacterControls.Move.canceled += onMovementInput;
    45.         playerInput.CharacterControls.Move.performed += onMovementInput;
    46.         playerInput.CharacterControls.Run.started += onRun;
    47.         playerInput.CharacterControls.Run.canceled += onRun;
    48.         playerInput.CharacterControls.Jump.started += onJump;
    49.         playerInput.CharacterControls.Jump.canceled += onJump;
    50.  
    51.         setupJumpVariables();
    52.     }
    53.  
    54.     void setupJumpVariables()
    55.     {
    56.         float timeToApex = maxJumpTime / 2;
    57.         gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
    58.         initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
    59.     }
    60.  
    61.     void handleJump()
    62.     {
    63.         if (!isJumping && characterController.isGrounded && isJumpPressed){
    64.             isJumping = true;
    65.             currentMovement.y = initialJumpVelocity;
    66.             currentRunMovement.y = initialJumpVelocity;
    67.         }
    68.     }
    69.  
    70.     void onJump(InputAction.CallbackContext context)
    71.     {
    72.         isJumpPressed = context.ReadValueAsButton();
    73.     }
    74.  
    75.     void onRun(InputAction.CallbackContext context)
    76.     {
    77.         isRunPressed = context.ReadValueAsButton();
    78.     }
    79.  
    80.     void handleRotation()
    81.     {
    82.         Vector3 positionToLookAt;
    83.  
    84.         positionToLookAt.x = currentMovement.x;
    85.         positionToLookAt.y = 0.0f;
    86.         positionToLookAt.z = currentMovement.z;
    87.         Quaternion currentRotation = transform.rotation;
    88.  
    89.      
    90.  
    91.         if (isMovementPressed) {
    92.             Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    93.             transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame);
    94.         }
    95.     }
    96.  
    97.     void onMovementInput(InputAction.CallbackContext context)
    98.     {
    99.         currentMovementInput = context.ReadValue<Vector2>();
    100.         currentMovement.x = currentMovementInput.x;
    101.         currentMovement.z = currentMovementInput.y;
    102.         currentRunMovement.x = currentMovementInput.x * runMultiplier;
    103.         currentRunMovement.z = currentMovementInput.y * runMultiplier;
    104.         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    105.     }
    106.  
    107.     void handleAnimation()
    108.     {
    109.         bool isWalking = animator.GetBool(isWalkingHash);
    110.         bool isRunning = animator.GetBool(isRunningHash);
    111.  
    112.         if(isMovementPressed && !isWalking) {
    113.             animator.SetBool(isWalkingHash, true);
    114.         }
    115.  
    116.         else if (!isMovementPressed && isWalking) {
    117.             animator.SetBool(isWalkingHash, false);
    118.         }
    119.  
    120.         if ((isMovementPressed && isRunPressed) && !isRunning)
    121.         {
    122.             animator.SetBool(isRunningHash, true);
    123.         }
    124.  
    125.         else if ((!isMovementPressed || !isRunPressed) && isRunning) {
    126.             animator.SetBool(isRunningHash, false);
    127.         }
    128.     }
    129.  
    130.     void handleGravity()
    131.     {
    132.         if(characterController.isGrounded){
    133.             currentMovement.y = groundedGravity;
    134.             currentRunMovement.y = groundedGravity;
    135.         } else {
    136.             currentMovement.y += gravity * Time.deltaTime;
    137.             currentRunMovement.y += gravity * Time.deltaTime;
    138.         }
    139.     }
    140.  
    141.     // Update is called once per frame
    142.     void Update()
    143.     {
    144.         handleRotation();
    145.         handleAnimation();
    146.  
    147.         if (isRunPressed){
    148.             characterController.Move(currentRunMovement * Time.deltaTime);
    149.         } else {
    150.              characterController.Move(currentMovement * Time.deltaTime);
    151.         }
    152.         handleGravity();
    153.         handleJump();
    154.     }
    155.  
    156.     void OnEnable()
    157.     {
    158.         playerInput.CharacterControls.Enable();
    159.     }
    160.  
    161.     void OnDisable()
    162.     {
    163.         playerInput.CharacterControls.Disable();
    164.     }
    165. }
    166.  
     
    Last edited: Sep 4, 2021
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    7,961
    You did not. The whole error message contains line number and column number too what you redacted from the error message.
    What is
    PlayerInput
    here? This one? If this one, why are you using this way? What's wrong with the official way? See the documentation I linked above.
    If it's not and you generated your class through the asset, I really strongly advise you to change the name of your class, because it is ambiguous.
     
    Last edited: Sep 5, 2021
  5. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    The line of code that it says is in question is 48 and 49. I am following a tutorial by iHeartGameDev on YouTube and I have set the perimeters just as he suggests in the previous tutorials, So I am not understanding in what way I should change the class. Also why would walk and run work fine if that were the case? This is the third or fourth episode in a string of tutorials and this is the first time that I have encountered a problem that wasn't a typo or something that I forgot to write in the script and was easily fixed. Somewhere along the line the jump action in the PlayerInput window is not registering. As shown in the original post the image is clear that I have set up the jump action in the same manner as the walk and run actions. I have also set the bindings to different buttons then walk and run.

    As far as to why the PlayerInput is used in this manner that would be something to inquire with the iHeartGameDev tutor Nicky.
     
    Last edited: Sep 6, 2021
  6. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    I am getting a warning message that might have to do with what you are saying about the PlayerInput but it doesn't stop the PlayMode, it just comes up as this warning in the console:
    Assets\StarterAssets\InputSystem\PlayerInput.cs(244,41): warning CS0436: The type 'PlayerInput' in 'C:\Users\King\Game demo\Assets\StarterAssets\InputSystem\PlayerInput.cs' conflicts with the imported type 'PlayerInput' in 'Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'C:\Users\King\Game demo\Assets\StarterAssets\InputSystem\PlayerInput.cs'.
     
  7. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    Why did you stop replying?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,067
    It may have to do with posts like this one:

    https://forum.unity.com/threads/adding-to-animation-script.1128152/#post-7257935

    But the past is the past, and I'm going to try to explain what I believe might be your issue above.

    I believe the warning is because you have two different classes named
    PlayerInput
    , one of them in your project as a script and perhaps one of them in some other DLL or external package. I'm honestly surprised it even lets the program run under those conditions, but as you say it appears just as a warning.

    I believe you could safely ignore the warning if it appears to run. The danger I would see is if the PlayerInput that it is being NOT chosen to run contains some functionality you need, it would not be present.

    One strategy might be to rename your own PlayerInput class to something else, being careful to rename the filename itself to match precisely (upper/lower case and all), and making sure all uses of that class are still valid (in scenes and prefabs). If you see a use that shows "Missing" you would need to re-drag and potentially repopulate any serialized data fields.

    Best of luck.
     
  9. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    38
    The warning has been resolved. I did have two separate scripts with the same name PlayerInput, deleted them both and started over. I also figured out that I had to delete the input system and create a new one, now the script recognizes the Jump command in the Input system, but the animation doesn't work, so I am going to delete and re-download the Mixamo animation and go from there.

    What does a statement I made to you a while back have anything to do with why Lurking Ninja stopped replying?
     
unityunity