- Home /
"Make field readonly" error. Private variables have to be readonly now?
Every time I make a private variable visual studio gives me an error saying "Make field readonly." I have never had this issue before. Is this new with Unity 2019? Or is it on visual studio's end? Does making a variable readonly affect anything?
Answer by Bunny83 · Jun 03, 2019 at 03:07 PM
I'm pretty sure you don't get an error but most likely a warning / suggestion. I'm pretty sure it's "IDE0044" which is triggered by the visual studio code analyzer when it detects a private variable that is either uninitialized or is only assigned either by a field initializer or inside the constructor of the class. Since no body else (usually) can assign a value to that field the cody analyzer suggest that you make the field read only.
Making a field read only will prevent that you can assign any new value to that field. You barely need readonly fields unless they are actual constants.
Without more context we can't suggest any actions you should / might take. Since it's just a warning / notice you can simply ignore it.
Answer by Maxence_Marchand · Jun 03, 2019 at 02:52 PM
Do you use (change the value) of that private variable in the code, or is it just one value that stays the same through the code?
Making a variable readonly does what you would expect : it only allows you to read its value. So if you want to change it within the code, it's better to not set it to readonly.
Also, what are your Visual Studio and Unity versions?
I am using the latest versions of both and my error console is filling up with 0044, even though I am constantly changing the value in the update loop.
Your answer
Follow this Question
Related Questions
Need Help With My GAME! 3 Answers
Changing variable values not having any effect 2 Answers
How do I combine a void with an iteration? 1 Answer
C# Return Type Error? 1 Answer
Check if a property has changed value while serialized 0 Answers