Bigi32
Bigi32

Reputation: 33

How to use interface in unity

I made an interface that looks like this:

public interface Weapon
{
    void Shoot();
    float damage { get; set; }
}

And I implement it in another class and for damage field I have something like this:

   float Weapon.damage
    {
        get
        {
            return damage;
        }

        set
        {
            throw new NotImplementedException();
        }
    }

But How can I use the getter for Weapon.damage ? I tried something like this: Weapon.damage.get() but it didn't work

Any help will be much appreciated, Thanks!

Upvotes: 2

Views: 2067

Answers (1)

Zakk Diaz
Zakk Diaz

Reputation: 1093

I agree with Code-Apprentice - you should take a look at other examples of fundamentals of classes & interfaces. For your specific item, you could do something like the following. I haven't tried to compile it but it should get you close

public interface IWeapon
{
    void Shoot();
    float Damage { get; }
}

public class Sword : IWeapon
{
    public void Shoot() { } //does nothing
    private float _damage { get; set; }
    public float Damage { get { return _damage; } }
    public Sword(int damage)
    {
        _damage = damage;
    }
}

Upvotes: 6

Related Questions