简体   繁体   English

启动函数未调用C#

[英]Start function not being called C#

I get a component like this 我得到这样的组件

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

where current aura is 当前光环在哪里

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

the variable currentAura itself is not null but all the properties are.. I don't understand why the Start function is not being called and initialise the properties properly, how can I fix this ? 变量currentAura本身不是null,而是所有属性。.我不明白为什么未调用Start函数并正确初始化属性,我该如何解决?

To have a function automatically called in your class you need to create a class contructor like below (the function has to be public, have no return type and be the same name as your class): 要在您的类中自动调用一个函数,您需要创建一个如下所示的类构造器(该函数必须是公共的,没有返回类型,并且必须与您的类同名):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM