错误:属性或索引器“CharacterStats.currentHealth”不能在此上下文中使用,因为set访问器不可访问

时间:2018-04-06 17:52:37

标签: c# unity3d

我正在制作一个食物/水系统,所以当你在食物或水上0时,你会失去一些健康,当你两个都是0时,这会更快。但我不断收到此错误“错误:属性或索引器”CharacterStats.currentHealth“不能在此上下文中使用,因为set访问器不可访问。”这是我的脚本,你们可以帮助我,下面是我的脚本。

    KCL_ID smallestDays  BMI
1     21          582     32.0
2     22          183     25.7

第二---------------------------------------------- -------------------------------

public class PlayerStats : CharacterStats
{
    public string ID{get;protected set;}
    public Texture healthIcon;
    public Texture waterIcon;
    public Texture foodIcon;
    public float water = 100;
    public float food = 100;
    // Use this for initialization
    void Start()
    {
        EquipmentManager.instance.onEquipmentChanged += OnEquipmentChanged;
    }

    void OnEquipmentChanged(Equipment newItem, Equipment oldItem)
    {
        if (newItem != null)
        {
            armor.AddModifier(newItem.armorModifier);
            damage.AddModifier(newItem.damageModifier);
        }

        if (oldItem != null)
        {
            armor.RemoveModifier(oldItem.armorModifier);
            damage.RemoveModifier(oldItem.damageModifier);
        }
    }

    public override void Die()
    {
        base.Die();
        //Kill the player in some way
        PlayerManager.instance.KillPlayer();
    }

    public void OnGUI()
    {
        //GUIStyle style = "box";

        GUIStyle style = "box";
        var healthstring = currentHealth.ToString("0");
        var waterstring = water.ToString("0");
        var foodstring = food.ToString("0");

        //Health
        GUI.Label(new Rect(10, 10, 100, 30), healthstring, style);
        GUI.DrawTexture(new Rect(15, 12, 100 / 4, 25), healthIcon, ScaleMode.StretchToFill, true, 10f);
        //Water
        GUI.Label(new Rect(240, 10, 100, 30), waterstring, style);
        GUI.DrawTexture(new Rect(245, 12, 100 / 4, 25), waterIcon, ScaleMode.StretchToFill, true, 10f);
        //Food
        GUI.Label(new Rect(355, 10, 100, 30), foodstring, style);
        GUI.DrawTexture(new Rect(360, 12, 100 / 4, 25), foodIcon, ScaleMode.StretchToFill, true, 10f);
    }

    public void Update()
    {
       if(water <= 0)
        {
            Debug.Log("Losing food");
            currentHealth = currentHealth - 1;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是因为你有private set。私有集的问题是该值只能在包含类型CharacterStats中更改,而不能在派生类型PlayerStats中更改。

public class CharacterStats
{
    public float Health {get; private set;}
    public float HealthA2 {get; set;}

    public CharacterStats()
    {
        Health = 100;//I can change the value in the constructor.  Making this immutable
    }

    public void DoWork()
    {
        Health = 75;//I can again change the value after construction so immutable not so much after all
    }
}

public class PlayerStats : CharacterStats
{
    public void MoreWork()
    {
        HealthA2 = 50;//This works
        //Health = 50;//ERROR:  I cannot change a private set in the derived class.  For that I need at least protected set;
    }
}

另见Immutable。并accessibility levels