Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:属性或索引器";CharacterStats.currentHealth“;无法在此上下文中使用,因为集合访问器不可访问_C#_Unity3d - Fatal编程技术网

C# 错误:属性或索引器";CharacterStats.currentHealth“;无法在此上下文中使用,因为集合访问器不可访问

C# 错误:属性或索引器";CharacterStats.currentHealth“;无法在此上下文中使用,因为集合访问器不可访问,c#,unity3d,C#,Unity3d,我正在制作一个食物/水系统,所以当你在食物或水上为0时,你会失去一些健康,当你在这两个方面都为0时,这会更快。但是我一直收到这样一个错误:“Error:property或indexer”CharacterStats.currentHealth“不能在这个上下文中使用,因为set访问器是不可访问的。”这是我的脚本你们能帮我吗,下面是我的脚本 public class PlayerStats : CharacterStats { public string ID{get;protected

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

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;
        }
    }
}
公共类PlayerStats:CharacterStats
{
公共字符串ID{get;protected set;}
公共纹理健康图标;
公共纹理水象;
公共食品卫生;
公众浮水=100;
公共浮动食品=100;
//用于初始化
void Start()
{
EquipmentManager.instance.OneEquipmentChanged+=OneEquipmentChanged;
}
作废已更改的设备(设备新项、设备旧项)
{
if(newItem!=null)
{
装甲.AddModifier(新装备.armorModifier);
损坏。添加修改器(新建项。损坏修改器);
}
if(oldItem!=null)
{
装甲。移除修改器(旧物品。装甲修改器);
损坏。移除修改器(旧项。损坏修改器);
}
}
公共覆盖无效死()
{
base.Die();
//以某种方式杀死玩家
PlayerManager.instance.KillPlayer();
}
公营机构
{
//GUIStyle=“box”;
GUIStyle=“box”;
var healthstring=currentHealth.ToString(“0”);
var waterstring=water.ToString(“0”);
var foodstring=food.ToString(“0”);
//健康
标签(新的Rect(10,10,100,30),healthstring,style);
DrawTexture(新的Rect(15,12,100/4,25),healthIcon,ScaleMode.StretchToFill,true,10f);
//水
标签(新的矩形(240,10,100,30),水串,样式);
DrawTexture(新的Rect(245,12100/4,25),waterIcon,ScaleMode.StretchToFill,true,10f);
//食物
标签(新的矩形(355,10,100,30),食物串,样式);
DrawTexture(新的Rect(360,12,100/4,25),foodIcon,ScaleMode.StretchToFill,true,10f);
}
公共无效更新()
{

如果(water它不起作用的原因是因为您有一个
私有集
。私有集的问题是,只能在包含类型
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;
    }
}

另请参见.And.

它不起作用的原因是您有一个
私有集
。私有集的问题是该值只能在包含类型
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;
    }
}

另请参见。和。

Uh…您是否考虑过不将集访问器
currentHealth
设置为private
public float currentHealth{get;private set;}
应该是
public float currentHealth{get;protected set;}
如果您希望派生类能够修改它。我对编码和c#是新手,因此如果出现这样的错误,我通常不知道如何解决,所以感谢大家!呃……您是否考虑过不为
currentHealth
设置集访问器
private
公共浮点currentHealth{get;private set;}
应该是
public float currentHealth{get;protected set;}
如果你想让派生类能够修改它。我对编码和c#不熟悉,所以如果出现这样的错误,我通常不知道如何解决,所以感谢大家!谢谢!当我有你们在这里时,我能问另一个问题吗?“GUI.Label(new Rect(355,10100,30),食物串,风格)如果我想让它居中,我应该只使用Screen.height/2和盒子一半的大小,宽度相同,还是有更简单的解决方案?@OlavAuslandOnstad-有很多优秀的专家在上面。发布一个新问题,一群人会很乐意提供帮助。谢谢!当我有你在这里时,我可以问另一个问题吗?“GUI.Label(新的Rect(355,10,100,30),foodstring,style);”如果我想让它居中,我应该只使用Screen.height/2和盒子的一半大小,宽度相同,还是有一个更简单的解决方案?@OlavAuslandOnstad-有很多优秀的专家。发布一个新问题,一群人会很乐意提供帮助。