【问题标题】:ERROR: The property or indexer "CharacterStats.currentHealth" cannot be used in this context because the set accessor is inaccessible错误:无法在此上下文中使用属性或索引器“CharacterStats.currentHealth”,因为 set 访问器不可访问
【发布时间】:2018-04-07 02:13:20
【问题描述】:

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

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;
        }
    }
}

第 2---------------------------------- -------------------------------------------

public class CharacterStats : MonoBehaviour {

    public int maxHealth = 100;
    public float currentHealth { get; private set; }

    public Stat damage;
    public Stat armor;

    void Awake()
    {
        currentHealth = maxHealth;
    }

    public void TakeDamage(int damage)
    {
        damage -= armor.GetValue();
        damage = Mathf.Clamp(damage, 0, int.MaxValue);


        currentHealth -= damage;
        Debug.Log(transform.name + " takes " + damage + " damage");

        if(currentHealth <= 0)
        {
            Die();
        }
    }

    public virtual void Die()
    {
        Debug.Log(transform.name + " died.");
    }
}

【问题讨论】:

  • 呃...您是否考虑过不为currentHealth 设置访问器private
  • public float currentHealth { get; private set; } 应该是 public float currentHealth { get; protected set; } 如果您希望派生类能够修改它。
  • 我是编码和 c# 的新手,所以如果出现这样的错误,我通常不知道如何解决,所以谢谢大家!

标签: c# unity3d


【解决方案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

【讨论】:

  • 谢谢!有你在,我可以再问一个问题吗? "GUI.Label(new Rect(355, 10, 100, 30), foodstring, style);"如果我想要这个居中,我应该只使用 Screen.height / 2 和盒子的一半大小,宽度也一样,还是有更简单的解决方案?
  • @OlavAuslandOnstad - SO上有很多优秀的专家。发布一个新问题,会有很多人很乐意提供帮助。
猜你喜欢
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
相关资源
最近更新 更多