找不到合适的方法来覆盖 OnInteract c#

No Suitable method found to override OnInteract c#

我正在尝试在 Unity 中构建我的游戏,在使箱子和路标可交互的过程中,我突然遇到错误。错误一直说“找不到合适的方法来覆盖”

我试过查看自己的代码并更改名称。我认为这与我将其命名为"Character character"有关。虽然我对此有 public 无效。我不断收到同样的错误。

这是我打开和更改箱子精灵的代码。

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

public class InteractableChest : InteractableBase
{
    public Sprite OpenChestSprite;
    public ItemType ItemInChest;
    public int Amount;

    private bool m_IsOpen;
    private SpriteRenderer m_Renderer;

    void Awake()
    {
        m_Renderer = GetComponentInChildren<SpriteRenderer>();
    }

    public override void OnInteract( Character character )
    {
        if( m_IsOpen == true )
        {
            return;
        }

        character.Inventory.AddItem( ItemInChest, Amount, PickupType.FromChest );
        m_Renderer.sprite = OpenChestSprite;
        m_IsOpen = true;
    }
}

问题似乎在

public override void OnInteract( Character character )
    {
        if( m_IsOpen == true )
        {
            return;
        }

因为所有 "Character character" 的文件都受此影响。 在我的 Characterinteractionmodel 文档中,我制作了以下代码片段:

[RequireComponent( typeof ( Character ) ) ]
public class CharacterInteractionModel : MonoBehaviour
{
    private Character m_Character;
    private Collider2D m_Collider;
    private CharacterMovementModel m_MovementModel;

    // Start is called before the first frame update
    void Awake()
    {
        m_Character = GetComponent<Character>();
        m_Collider = GetComponent<Collider2D>();
        m_MovementModel = GetComponent<CharacterMovementModel>();
    }

    // Update is called once per frame
    void Update()
    {

    }


    public void OnInteract()
    {
        InteractableBase usableInteractable = FindUsableInteractable();

        if( usableInteractable == null )
        {
          return;
        }

        usableInteractable.OnInteract( m_Character );

    }

在所有损坏的文件(其中包含字符的文件)中,我有相同的错误,指出“错误 CS0115:'InteractableChest.OnInteract(Character)':找不到合适的方法来覆盖

Interactbase 文档:

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

public class InteractableBase : MonoBehaviour
{
    virtual public void OnInteract()
    {
        Debug.LogWarning( "OnInteract is not implemented" );
    }
}

为了覆盖一个方法,您需要匹配它的签名并且您的基础 class 没有参数列表:

virtual public void OnInteract()

您的派生 class 需要一个 Character 参数:

public override void OnInteract( Character character )

因此,为了修复它,您需要在基础 class 的方法中使用该参数,即使它不使用它:

virtual public void OnInteract( Character character )