- Home /
How to serialized a variable in a c# method?
I was wondering how to serialized a field in a c# method so that I can change the variable through the Unity Inspector rather than having to constantly go back to the code to change it. I know how to do this for class variables ([Serialize Field] or make the variable public) but when I try to do it for in-method variables I get an error.
Answer by Jeff-Kesselman · May 06, 2014 at 12:34 AM
Oh, sorry, misread your question.
Short answer is you cannot expose anything that isn't in the class global scope directly.
In C# it wouldn't make any sense if you tried, since everything defined within a method is a temporary variable that only exists as long as the method is executing.
Why not put the value in a global scope public field and then instantiate your method variable (stack variable) from it?
Yeah I guess that is what I'll have to do. I was just curious if there was any way to do it all inside the method. Thanks!
Your answer
Follow this Question
Related Questions
usage of FieldInfo.SetValue with SerializeField 2 Answers
Why does the prefabOverride not work on fields marked as SerializeField? 0 Answers
MethodInfo listing functions and parameters of a public variable 2 Answers
How to update serializedField showed on the inspector by script? 1 Answer
How to properly serialize fields and store them for use in play mode? 1 Answer