简体   繁体   中英

I'm getting this error: The type or namespace 'Action' could not be found (are you missing a using directive or an assembly reference?)

I'm getting this error on public event Action OnBeforeMove() and I don't know why. What am I missing?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
    [SerializeField] float mouseSensitivity = 3f;
    [SerializeField] float movementSpeed = 10f;
    [SerializeField] float jumpSpeed = 5f;
    [SerializeField] float mass = 1f;
    [SerializeField] float acceleration = 20f;
    [SerializeField] Transform cameraTransform;

    public event Action OnBeforeMove;
    
    internal float movementSpeedMultiplier;

    CharacterController controller;
    Vector3 velocity;
    Vector2 look;

    PlayerInput playerInput;
    InputAction moveAction;
    InputAction lookAction;
    InputAction jumpAction;
    InputAction sprintAction;

    void Awake()
    {
        controller = GetComponent<CharacterController>();
        playerInput = GetComponent<PlayerInput>();
        moveAction = playerInput.actions["move"];
        lookAction = playerInput.actions["look"];
        jumpAction = playerInput.actions["jump"];
        sprintAction = playerInput.actions["sprint"];
    }

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        UpdateGravity();
        UpdateMovement();
        UpdateLook();
    }

    void UpdateGravity()
    {
        var gravity = Physics.gravity * mass * Time.deltaTime;
        velocity.y = controller.isGrounded ? -1f : velocity.y + gravity.y;
    }

    Vector3 GetMovementInput()
    {
        var moveInput = moveAction.ReadValue<Vector2>();

        var input = new Vector3();
        input += transform.forward * moveInput.y;
        input += transform.right * moveInput.x;
        input = Vector3.ClampMagnitude(input, 1f);
        input *= movementSpeed * movementSpeedMultiplier;
        return input;
    }

    void UpdateMovement()
    {
        movementSpeedMultiplier = 1f;
        OnBeforeMove?.Invoke();

        var input = GetMovementInput();

        var factor = acceleration * Time.deltaTime;
        velocity.x = Mathf.Lerp(velocity.x, input.x, factor);
        velocity.z = Mathf.Lerp(velocity.z, input.z, factor);

        var jumpInput = jumpAction.ReadValue<float>();
        if (jumpInput > 0 && controller.isGrounded)
        {
            velocity.y += jumpSpeed;
        }

        controller.Move(velocity * Time.deltaTime);
    }

    void UpdateLook()
    {
        var lookInput = lookAction.ReadValue<Vector2>();
        look.x += lookInput.x * mouseSensitivity;
        look.y += lookInput.y * mouseSensitivity;

        look.y = Mathf.Clamp(look.y, -110f, 110f);

        cameraTransform.localRotation = Quaternion.Euler(-look.y, 0, 0);
        transform.localRotation = Quaternion.Euler(0, look.x, 0);
    }
}

I was trying to create an event that basically gives a "warning" that it's about to do the movement calculation logic. I've imported the Input System so it should have worked, but I'm obviously doing something wrong.

Please add using System; at the top of your class.

See this reference: https://learn.microsoft.com/en-us/dotnet/api/system.action-1?view=net-7.0

Action is part of the System namespace; System.Action OnBeforeMove .

The error you get is where the compiler is when realising something is not right. Therefore you should examine that line and character.

What is OnbeforeMove , it is an System.Action .

Why does the compiler nag about it, there is no reference anywhere to System .

The compiler just doesn't know what to do but to inform you about what it doesn't know:

The type or namespace 'Action' ... the compiler does not even know if Action is a type or namespace. And both is an assumption by the compiler.

(are you missing a using directive or an assembly reference?)

That means exactly what is going on here. The assembly reference to System is missing. Most likely you did add the System.Runtime.dll as VS does this for you. But having either System. before Action or the use of the using System at the top of the file is needed.

If you get this error:

  1. Did I make a typo!

  2. Did I add a reference?

  3. Is the assambly added at all.

  4. Check the corresponding error page at learn.microsoft.com.

    (click the link of the error code in the error-list of VS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM