1

Salutations, this'll be brief.

So, I tried to change the name of one the hero struct in my game, but it doesn't update, neither in the inspector nor in the de facto code.

I can call the constructor just fine, and if I print the heroname before and after (in the constructor), it tells me the new name. However, It does not change.

Here is the (simplified) code:

//This already has a name in the inspector that I want to override
public List<TroopStat> PlayerHeroStats = new List<TroopStat>();    


void Start () {
    PlayerHeroStats[0].ChangeTroopType();
}

[System.Serializable]
public struct TroopStat {
    public string nameOfTroop;

    public void ChangeTroopType() {
        nameOfTroop = "Blabla";
    }
}

Any ideas?

1 Answers1

2

Structs are value types. You need to assign a new struct or use class instead.

This should work:

void Start () {
    TroopStat stat = PlayerHeroStats[0]; 
    stat.ChangeTroopType();
    PlayerHeroStats[0] = stat;
}

Or make the TroopStat a class.

You can read more about it here.

Dave
  • 2,684
  • 2
  • 21
  • 38
  • 1
    Thank you very much, kind sir. Works like a charm. Decided against using that as a class for clarity reasons in the inspector :D – Schneeritter Sep 01 '19 at 15:54