Does not implement interface member

Hey i’m getting this error message and i’m not too sure whats causing it.

Assets\Scripts\Combat\CombatTarget.cs(12,48): error CS0535: 'CombatTarget' does not implement interface member 'IRaycastable.HandleRaycast(PlayerController)'
Assets\Scripts\Combat\WeaponPickup.cs(9,48): error CS0535: 'WeaponPickup' does not implement interface member 'IRaycastable.HandleRaycast(PlayerController)'

using System;
using System.Collections;
using System.Collections.Generic;
using RPG.Control;
using UnityEngine;

namespace RPG.Combat 
{
    public class WeaponPickup : MonoBehaviour, IRaycastable
    {
            [SerializeField] Weapon weapon = null;
            [SerializeField] float respawnTime = 5;

            private void OnTriggerEnter(Collider other)
            {
                if (other.gameObject.tag == "Player")
                {
                    Pickup(other.GetComponent<Fighter>());
                }
            }
        private void Pickup(Fighter fighter)
        {
            fighter.EquipWeapon(weapon);
            StartCoroutine(HideForSeconds(respawnTime));
        }

        private IEnumerator HideForSeconds(float seconds)
        {
            ShowPickup(false);
            yield return new WaitForSeconds(seconds);
            ShowPickup(true);
        }
        private void ShowPickup(bool shouldShow)
        {
            GetComponent<Collider>().enabled = shouldShow;
            foreach (Transform child in transform)
            {
                child.gameObject.SetActive(shouldShow);
            }
        }

        public bool HandleRaycast(PlayerController callingController)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Pickup(callingController.GetComponent<Fighter>());
            }
            return true;
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;
using RPG.Attributes;
using RPG.Control;

namespace RPG.Combat
{
    [RequireComponent(typeof(Health))]
    public class CombatTarget : MonoBehaviour, IRaycastable
    {
        public bool HandleRaycast(PlayerController callingController)
        {
            if (!callingController.GetComponent<Fighter>().CanAttack(gameObject))
            {
                return false;
            }

            if (Input.GetMouseButton(0))
            {
                callingController.GetComponent<Fighter>().Attack(gameObject);
            }

            return true;
        }
    }
}

namespace RPG.Control
{
    public interface IRaycastable
    {
        bool HandleRaycast(PlayerController callingController);
    }
} 

I think it’s because you’re not doing “IRaycastable.HandleRaycast” since the interface member is not public. Try adding in the “IRaycastable.” infront of the HandleRaycast methods in each script.

This is strange. Do you perhaps have 2 PlayerController scripts? You can have 2 if the namespaces don’t match.

The only reason I can see/think of for this error is that the PlayerController referenced by the interface is not the same one referenced by the classes implementing it, meaning the signatures don’t match because the types don’t match.

I tried what you suggested and I’m running into some more errors, Thanks for taking the time to help me.

Assets\Scripts\Combat\CombatTarget.cs(14,27): error CS0539: 'CombatTarget.HandleRaycast(PlayerController)' in explicit interface declaration is not found among members of the interface that can be implemented

Assets\Scripts\Control\IRaycastable.cs(5,14): error CS0540: 'IRaycastable.HandleRaycast(PlayerController)': containing type does not implement interface 'IRaycastable'

Assets\Scripts\Control\IRaycastable.cs(5,27): error CS0501: 'IRaycastable.HandleRaycast(PlayerController)' must declare a body because it is not marked abstract, extern, or partial

Assets\Scripts\Combat\WeaponPickup.cs(44,27): error CS0539: 'WeaponPickup.HandleRaycast(PlayerController)' in explicit interface declaration is not found among members of the interface that can be implemented

Back out the changes that Brandon suggested. I don’t believe that this is the issue. In an interface declaration, the method or property is never public. The requirement is that the member be accessible to whatever class is calling it through the interface, which is why we must declare it as public or with a qualifier in implementations of the interface.

So everything looks absolutely correct here, which is definitely confusing.

Is the error appearing in both your code editor and Unity, just the code editor or just in Unity?

1 Like

I am only getting the error message in unity and am not seeing errors in my code editor.

Ok, so there is a possibility that Unity isn’t properly updating the script internally… this is an occassional “caching” bug that both Unity and Unreal suffer from time to time.

  • Close Unity and Visual Studio Code
  • Go into your project folder and delete the Library folder
  • Reopen Unity
    This will force a recompile within Unity.

Okay I tried it and I’m still getting the error message

Zip up your project and upload it to https://gdev.tv/projectupload and I’ll see if I can figure out what’s going on. Be sure to remove the Library folder from the zip before uploading.

You have two PlayerController scripts in your project. One is our PlayerController, the other is a 3rd party asset in AssetPacks/FXIFIED. The interface implementation in your CombatTarget.cs does not specify which PlayerController to use (one is in the global namespace, and one is in Control).

I’m not sure what the FXIFIED is for. One easy solution would be to qualify the PlayerController reference in your HandleRaycast method:

        public bool HandleRaycast(RPG.Control.PlayerController callingController)
        {
            if (!callingController.GetComponent<Fighter>().CanAttack(gameObject))
            {
                return false;
            }

            if (Input.GetMouseButton(0))
            {
                callingController.GetComponent<Fighter>().Attack(gameObject);
            }

            return true;
        }

Ideally, I tend to strip the scripts from 3rd party components unless I really need them (and you don’t, though this may break some prefabs from this 3rd party asset pack if you delete the scripts.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms