Reputation: 99
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. 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:
[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
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" );
}
}
Upvotes: 1
Views: 3255
Reputation: 9821
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:
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 )
Upvotes: 1