崩溃团结的构造函数

时间:2018-04-22 22:46:58

标签: c# unity3d crash

代码

Army.Add(new PigSquad("The Old Guard", 10, 4));

其中ArmyList<Unit>Pigsquad定义为

public class PigSquad : Unit
{
    public override bool Dead { get; set; }
    public override string Name { get; set;}
    public override int Strength { get; set; }
    public override int Age { get { return Age; } set { Age = value; Strength += 20 - Age; if (Age > 20) { Dead = true; } } }
    public override int Veterancy { get; set; }
    public PigSquad()
    {

    }
    public PigSquad(string Name)
    {
        this.Name = Name;
    }
    public PigSquad(string Name, int Strength)
    {
        this.Name = Name;
        this.Strength = Strength;
    }
    public PigSquad(string Name, int Strength, int Age)
    {
        this.Name = Name;
        this.Strength = Strength;
        this.Age = Age;
    }
}

运行时崩溃Unity编辑器。我不知道为什么会这样。任何帮助都将非常感谢。

1 个答案:

答案 0 :(得分:3)

public override int Age { get { return Age; } set { Age = value; Strength += 20 - Age; if (Age > 20) { Dead = true; } } }

应该是

private int _Age; //maybe make this in your UNIT object as well and that way you do not need it in your child.
public override int Age { get { return _Age; } set { _Age = value; Strength += 20 - _Age; if (Age > 20) { Dead = true; } } }