- Home /
How do you rename a public variable and keep object references/values?
Whenever I rename a public variable, I lose all values/references of that variable in all objects with that component.
What's the unity way to rename public variables? I guess it's not with the IDE (unless monodevelop doesn't have this problem, I'm using visual studio).
Lol thanks, that WOULD fix the issue, but I don't want to use properties for every variable just so I can rename them later..
There must be a proper way to do this in unity. I'd be very surprised if there wasn't.. (If not I'd rather try writing an editor script for it before resorting to using properties for everything.)
Answer by PsychoDuckArcade · Apr 06, 2015 at 10:29 PM
I found a solution that's still quite tedious for simply renaming a variable but for as often as one needs to rename a variable it's worth it and works perfectly.
I found the attribute [FormerlySerializedAs("yourVariablesPreviousName")] in the UnityEngine.Serialization namespace.
1) Place the attribute before the variable you wish to rename.
2) Rename the variable and save changes.
3) Switch to the inspector, wait for the name to change, it will have reserialized with the new name and keep all values and references.
4) Remove the attribute (probably optional).
$$anonymous$$an that is SOOO awesome. I would never have believed there exists an answer. Jesus that is so fricking usefull, thanks A LOT!
Your answer
Follow this Question
Related Questions
Static and Coroutines help 2 Answers
Maintain variable values while changing levels 1 Answer
Modify a variable inside a game object from other object. 1 Answer
Creating a Prefab and Keeping Original Objects Script Information 0 Answers
Assign a random value to var personality" upon asset creation (Java) 1 Answer