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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, kind sir. Works like a charm. Decided against using that as a class for clarity reasons in the inspector :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.