【问题标题】:No Suitable method found to override OnInteract c#找不到合适的方法来覆盖 OnInteract c#
【发布时间】:2019-07-25 21:37:13
【问题描述】:

我正在尝试在 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 文档中,我制作了以下代码的 sn-p:

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

【问题讨论】:

    标签: c# unity3d 2d


    【解决方案1】:

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

    virtual public void OnInteract()
    

    您的派生类需要Character 参数:

    public override void OnInteract( Character character )
    

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

    virtual public void OnInteract( Character character )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多