Reputation: 8277
I have a basic class setup of Items that receive some damage and have a health status. But for some reason i cannot access the base variables as i get inaccessible error.
This is my code setup:
public abstract class Items
{
public float health { get; private set; }
public Items()
{
health = 100f;
}
}
public class Sword : Items
{
public string name { get; private set; }
public float maxPower { get; private set; }
public Sword(string n float mPower) : base()
{
name = n;
maxPower = mPower;
}
public void UpdateDamage(float damageAmount)
{
health = Mathf.Clamp(health - damageAmount,0,100);
}
}
The error is:
'Items.health' is inaccessible due to its protection level
I presumed because i set it to public i could access it in my damage method, but i guess not. Where did i go wrong here?
Upvotes: 1
Views: 547
Reputation: 37281
Change the setter of the Items
from private
to protected
. Because it is a private set
only that class can set it an non other, even not the derived classes.
protected set
(like below).public
) remove the word and leave only set;
- the default is public
For more information about the access modifiers
of properties check MSDN
So:
public abstract class Items
{
public float health { get; protected set; }
public Items()
{
health = 100f;
}
}
Upvotes: 3