启动函数未被调用C#

时间:2016-07-08 00:08:06

标签: c# function class unity3d properties

我得到了这样的组件

GameObject currentAuraObject
IAura currentAura = currentAuraObject.GetComponent<IAura>();

目前的光环是

public class AirAura : MonoBehaviour, IAura
{
    public ElementalIncreament DamageElementalIncreament { get; set; }
    public ElementalIncreament ResistanceElementalIncreament { get; set; }
    public ElementalIncreament EnemyElementIncreament { get; set; }

    private void Start()
    {
        DamageElementalIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Air, 20);
        ResistanceElementalIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Air, 15);
        EnemyElementIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Earth, 35);
    }
}

变量currentAura本身不是null但是所有属性都是..我不明白为什么没有调用Start函数并正确初始化属性,我该如何修复这个?

1 个答案:

答案 0 :(得分:3)

要在类中自动调用函数,您需要创建一个类构造函数,如下所示(该函数必须是公共的,没有返回类型,并且与您的类名称相同):

public AirAura()
    {
        DamageElementalIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Air, 20);
        ResistanceElementalIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Air, 15);
        EnemyElementIncreament = new ElementalIncreament(ElementalIncreament.ElementalType.Earth, 35);
    }