Search Unity

Bug Error CS8701 Target runtime doesn't support default interface implementation [SOLVED]

Discussion in 'Scripting' started by NimbleSprite, Jul 4, 2021.

  1. NimbleSprite

    NimbleSprite

    Joined:
    Nov 3, 2017
    Posts:
    34
    Thanks so much for any help with this. :)

    I am wondering why my C# script, in Unity, is not allowing a normal interface implementation. It is a script, not unlike any other script, inheriting from MonoBehaviour. I am using Visual Studio. I have updated Unity, a normal install of Unity and have certainly not changed any critical Visual Studio settings. Here is the implementation. In the script I simply try to declare an interface.

    public interface IRenderable
    {
    public bool Complete();
    public void OnGameFrame(float GFPS)
    {
    //do things
    }
    }

    Once I try adding this void it shows an error. It is OK with everything else (the interface declaration, and the bool). As in, if I comment out the void it no longer gives an error! Super confusing, and leaves me wondering am I missing something or don't understand something about interfaces in Unity? I looked online for solutions, and only found old threads saying to change the target framework of the Visual Studio solution (which did not help me as I use Unity).

    Hopefully this provides enough info but if not let me know, thanks very much!
     
    Last edited: Jul 4, 2021
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You can't declare what a function does in an Interface, only in the classes that implement it.
     
    NimbleSprite likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Bunny83 and NimbleSprite like this.
  4. NimbleSprite

    NimbleSprite

    Joined:
    Nov 3, 2017
    Posts:
    34
    If I understand both of you correctly, and according to the thread linked...there cannot be a virtual function of sorts directly in the interface. I get that, but how am I expected in Unity to create the desired effect of a common function inheritable / overridable? If anyone can answer that it would be really great, I just didn't foresee this being a problem, wow. Thanks for at least explaining its impossible

    EDIT: Just figured out the solution to my problem, thank you both kindly
     
    Last edited: Jul 4, 2021
    Kurt-Dekker likes this.
  5. brlan10

    brlan10

    Joined:
    Mar 23, 2015
    Posts:
    5
    Replying here because I had the same misunderstanding, and had to figure it out for myself despite NimbleSprite's incredibly helpful comment above.

    To solve the issue, simply remove the curly brackets from the method and end the line with a semicolon, as seen below

    public interface ITriggerable
    {
    public void Trigger(object sender, EventArgs e);
    }
     
    Hopfer1 and ZeHgS like this.
  6. RhinocerosGamesProduction

    RhinocerosGamesProduction

    Joined:
    Dec 19, 2017
    Posts:
    4
    Hey, what was the solution you found? Did you just use an abstract class instead?


    That is not a solution to this stated problem as it was intended to implement a function in an interface which is possible in default C#, java, etc. But apparently not (yet) in Unity..
     
  7. NimbleSprite

    NimbleSprite

    Joined:
    Nov 3, 2017
    Posts:
    34
    As RadRedPanda stated earlier in this thread...

    public interface IRenderable
    {
    public void OnGameFrame(float GFPS)
    {
    //declaring what a function does here, inside the interface, is not possible. I declared what the function does in a separate class that implements this interface.
    }
    }

    My interface looks like this now:

    public interface IRenderable
    {
    public bool Complete();

    public void OnGameFrame(int currentGFPS);
    }
     
  8. JohnnyConnor

    JohnnyConnor

    Joined:
    Aug 10, 2020
    Posts:
    42
  9. LeRondPoint

    LeRondPoint

    Joined:
    Jan 22, 2021
    Posts:
    10
    A proper solution would be to use an extension method on the interface.

    Code (CSharp):
    1.  
    2. public static class InterfaceExtension
    3. {
    4.     public static void InterfaceMethod(this IInterface self)
    5.     {
    6.         // Do your stuff on self
    7.     }
    8. }
    9.  
    Then you can use it

    Code (CSharp):
    1.  
    2. IInterface interface = new ConcreteClass();
    3.  
    4. interface.InterfaceMethod();
    5.  
     
    Last edited: May 13, 2022
    gameguru_unity4 and Riptide559 like this.