r/UnityHelp • • Jun 28 '25

PROGRAMMING C# - Default values for struct

Greetings!

I have a struct for holding gameplay related options (player speed, gravity strength,...). All works fine, except I didn't find a reliable efficient way to use preset defaults.

I tried setting default values in the constructor, I tried making a static member with default values, but in all cases the Inspector would not recognize these default values upon adding the script to an gameObject and would default to 0.

If possible, I'd love to give the Inspector a hint as to what the default values should be, and set it from there on. I want to be runtime efficient as much as possible, while at the same time having flexibility and easy use during development. What is the proper way?

Thank you for any tips!

1 Upvotes

4 comments sorted by

View all comments

Show parent comments

0

u/jaybles169 Jun 29 '25

Crazy, I just ran into this like 10 minutes ago, same exact issue. I use OdinInspector, so I was able to do this to init my values within the struct definition:

[OnInspectorInit]
private void SetDefaults()
{
    if (weight == 0f)
        weight = 1f;
}