简体   繁体   English

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

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

I'm trying to build my game in Unity and during the process of making the chest and signpost interactable, I suddenly got an error.我正在尝试在 Unity 中构建我的游戏,在使箱子和路标可交互的过程中,我突然出现错误。 The error keeps saying " No Suitable method found to override"错误一直说“找不到合适的方法来覆盖”

I've tried looking at my own code and changing the names.我试过查看我自己的代码并更改名称。 I think it has to do with me naming it "Character character".我认为这与我将其命名为“角色”有关。 While I do have a public void for this.虽然我对此有一个公共空白。 I keep getting the same error.我不断收到同样的错误。

This is the code I have for opening and changing the sprites of the chest.这是我用于打开和更改胸部精灵的代码。

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

The problem seems to be at问题似乎在

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

Since all the files with "Character character" are affected by this.由于所有具有“字符”的文件都受此影响。 In my Characterinteractionmodel document I made a snippet of the following code:在我的 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 );

    }

In all the corrupted files (The ones with the character in it) I have the same erorr which states "Error CS0115: 'InteractableChest.OnInteract(Character)': No Suitable Method Found to Override在所有损坏的文件(其中包含字符的文件)中,我有相同的错误,指出“错误 CS0115:'InteractableChest.OnInteract(字符)':找不到合适的方法来覆盖

Interactbase document:交互库文档:

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

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

In order to override a method, you need to match its signature and your base class doesn't have a parameter list:为了覆盖一个方法,你需要匹配它的签名并且你的基类没有参数列表:

virtual public void OnInteract()

Your derived class requires a Character parameter:您的派生类需要一个Character参数:

public override void OnInteract( Character character )

So in order to fix it, you need that parameter in you base class's method, even if it doesn't use it:因此,为了修复它,您需要在基类的方法中使用该参数,即使它不使用它:

virtual public void OnInteract( Character character )

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

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