繁体   English   中英

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

[英]No Suitable method found to override OnInteract c#

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

我试过查看我自己的代码并更改名称。 我认为这与我将其命名为“角色”有关。 虽然我对此有一个公共空白。 我不断收到同样的错误。

这是我用于打开和更改胸部精灵的代码。

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

由于所有具有“字符”的文件都受此影响。 在我的 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(字符)':找不到合适的方法来覆盖

交互库文档:

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

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

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

virtual public void OnInteract()

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

public override void OnInteract( Character character )

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

virtual public void OnInteract( Character character )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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