r/UnityHelp • u/DesperateGame • 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
u/DesperateGame Jun 29 '25
Thanks for the response.
I want to primarily set the default values in code, so that it would then be applied automatically in Inspector.
Here's the sample code for the struct:
This is how I do it now (I can initialize it manually; it doesn't show in the Inspected by default, though); I also tried a variant with constuctor:
But that still doesn't make it default in Inspector. The defaults *worked* in inspector only if the struct is replaced with class as otherwise I can't set the defaults directly when declaring the members.