skip to Main Content

I’m getting acquainted with the Polymorphism of OOP while making a game in Unity.

I try to use an interface that will take part in a damage system.

Here is the interface code:

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

public interface IDamageable 
{
    int Health { get; set;}
    void Damage();
}

And here is the code of a skeleton enemy, who inherits from the parent class Enemy and has to implement the IDamageable interface:

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

public class Skeleton : Enemy, IDamageable
{
    public int Health { get; set; }

    public override void Init()
    {
        base.Init();
        Health = base.health;
    }

    public void Damage()
    {
    }
}

So the thing is that the console in Unity doesn’t show any error messages regarding the interface.

But the VS Code keeps the IDamageable underlined with an error CS0246:

The type or namespace name ‘IDamageable’ could not be found (are you missing a using directive or an assembly reference?)

Looks like VS Code doesn’t even see the interface – can’t find the interface in intellisense either.

I tried to implement the interface for some other enemies, but the case is the same: no error in the unity console, and the interface is also marked by VS Code.

Sorry if that question is dumb!

2

Answers


  1. Chosen as BEST ANSWER

    Ok, it took time, but I found a solution to this issue.

    The thing is that a namespace is not a necessity in this case. I did read about namespaces but am not sure how to use them yet. And the course I follow doesn't use namespaces either.

    So about the issue. Due to the appearance of the error - it appears only in VS Code, I decided to do the following:

    1. While in Unity entered Preferences -> External tools
    2. Saw that in the field External Script Editor I had Open by file extension
    3. Changed that field to Visual Studio Code
    4. Pressed Regenerate project files

    The error CS0246 in VS Code disappeared, and the interface was implemented successfully.


  2. You have to recompile the script, see what’s up. If issue persists delete script, write it again. Most likely there is some BS with assembly

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search