我收到此错误:找不到类型或命名空间'Action'(您是否缺少 using 指令或程序集引用?



我在公共事件ActionOnBeforeMove()上得到这个错误,我不知道为什么。我错过了什么?

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);
}
}

我试图创建一个事件,基本上给出一个"警告";这就是运动计算逻辑。我已经导入了输入系统,所以它应该可以工作,但是我显然做错了什么。

请把using System;加在班级的最上面。

参见此参考:https://learn.microsoft.com/en-us/dotnet/api/system.action-1?view=net-7.0

ActionSystem命名空间的一部分;System.Action OnBeforeMove.

你得到的错误是当编译器意识到某些东西是不正确的。因此,您应该检查该行和字符。

什么是OnbeforeMove,它是System.Action

为什么编译器会唠叨,没有任何地方引用System

编译器不知道该做什么,只能告诉你它不知道的事情:

The type or namespace 'Action'…编译器甚至不知道Action是类型还是名称空间。并且both是编译器的假设。

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

这意味着这里正在发生的事情。缺少对System的程序集引用。最有可能的是你添加了System.Runtime.dll,因为VS为你做了这个。但是在Action之前有System.或者在文件的顶部使用using System是必要的。

如果你得到这个错误:

  1. 我打错字了吗?

  2. 我添加引用了吗?

  3. 是否添加了组件。

  4. 在learn.microsoft.com查看相应的错误页面

    (点击VS错误列表中的错误码链接)

相关内容