Visual Studio error for referenced scripts

Hi,

I switched over to Visual Studio recently and I always get an error whenever I have a public class referenced in another script. The error/warning message is:

Warning CS0649: Field ‘Script.referencedScript’ is never assigned to, and will always have its default value null (CS0649) (Assembly-CSharp).

even though in the script there are functions called from the referenced script.
Actually everything works just fine when running the game but I am still curious what these warnigns mean exactly and how I can get rid of them.

Thanks in advance :slight_smile:

Without a code example of the compiler warning we can only answer in generalities, but this warning is part of what Visual Studio brings to the development toolset (and there are lots of ways to get this feature).


What you’re seeing is the result of some code checking / analysis, and some tools go much deeper into studying the code we write to make suggestions and observations known to create bugs that aren’t obvious when we write. It isn’t always a good idea to ignore warnings, professional developers work to write code that passes all inspection from analysis tools, but here I speak of development outside of Unity in all manner of applications, many of which are critical. If you’re studying, consider the warnings of this type as an introduction into the kinds of things known by professionals and academics to be noteworthy, historically the source of bugs, but for you they may occasionally be ignored until you have a problem (crashes, errors, etc.). Sometimes these warnings give you some indication of why a crash has happened.


It would seem the compiler things there is a member variable that starts uninitialized, where the default for all class and struct types is null. If you access such a variable it would create a crash (null exception), but if you know you do initialize before accessing the value, or you know that you test for null before trying to use it, you’re generally covered.


As you become more advanced, these warnings should reduce in volume. They’re worth understanding and avoiding, and as you select options to analyze code or increase the warning level (an option to the compiler), the compiler starts to list a lot of warnings you may never have seen before. Full code analysis can even read to be insulting if you didn’t realize it was generated by a computer program :wink:

Have you downloaded any extions for the visual studio? like c# completer. sometimes a restart helps aswell.

but the error means that you have properties which are never used i guess!