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 ?

Upvotes: 0

Views: 314

Answers (1)

Ben Richards
Ben Richards

Reputation: 3575

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

Upvotes: 3

Related Questions