Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How do I fix this?

Discussion in 'Scripting' started by TrexMatrix, Sep 4, 2023.

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    I got this error and don't know what the problem is in my script
    Assets\Scripts\InputHandler.cs(32,31): error CS1061: 'CameraHandler' does not contain a definition for 'FollowTarget' and no accessible extension method 'FollowTarget' accepting a first argument of type 'CameraHandler' could be found (are you missing a using directive or an assembly reference?)

    Script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace HR
    7. {
    8.     public class InputHandler : MonoBehaviour
    9.     {
    10.         public float horizontal;
    11.         public float vertical;
    12.         public float moveAmount;
    13.         public float mouseX;
    14.         public float mouseY;
    15.  
    16.         PlayerControls inputActions;
    17.         CameraHandler cameraHandler;
    18.  
    19.         Vector2 movementInput;
    20.         Vector2 cameraInput;
    21.  
    22.         private void Awake()
    23.         {
    24.             cameraHandler = CameraHandler.singleton;
    25.         }
    26.  
    27.         private void FixedUpdate()
    28.         {
    29.             float delta = Time.fixedDeltaTime;
    30.  
    31.             if (cameraHandler != null)
    32.             {
    33.                 cameraHandler.FollowTarget(delta);
    34.                 cameraHandler.HandleCameraRotation(delta, mouseX, mouseY);
    35.             }
    36.         }
    37.  
    38.         public void OnEnable()
    39.         {
    40.             if (inputActions == null)
    41.             {
    42.                 inputActions = new PlayerControls();
    43.                 inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
    44.                 inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
    45.             }
    46.  
    47.             inputActions.Enable();
    48.         }
    49.  
    50.         private void OnDisable()
    51.         {
    52.             inputActions.Disable();
    53.         }
    54.  
    55.         public void TickInput(float delta)
    56.         {
    57.             MoveInput(delta);
    58.         }
    59.  
    60.         private void MoveInput(float delta)
    61.         {
    62.             horizontal = movementInput.x;
    63.             vertical = movementInput.y;
    64.             moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
    65.             mouseX = cameraInput.x;
    66.             mouseY = cameraInput.y;
    67.         }
    68.     }
    69. }
    70.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    The error is pretty clear. The CameraHandler type doesn't have a FollowTarget method.

    If you think it does, consider that the compiler is *never* wrong.
     
    Bunny83 and Kurt-Dekker like this.
  3. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    never mind i figured it out i forgot to add a public void in the camerahandler script
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,245
    Looks like you either forgot a step in the tutorial you are following or else you made a typo.

    You cannot do either of these things. When you do, here's how to fix it:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    void is the return type and nothing to do with the above. You mean it was private so it couldn't see it I presume so you made it public.
     
    Bunny83 likes this.
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I would probably read the error messages a little more carefully. As they usually are spot on with what's wrong.
     
    Yoreki and Kurt-Dekker like this.