- Home /
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 :)
Answer by JVene · Sep 17, 2018 at 01:36 PM
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 ;)
Thanks for this in depth answer!
To make it more clear: I basically have two scripts. ScriptA and ScriptB. ScriptA has some type of function let's say functionA.
In ScriptB I have a reference to ScriptA and for example in ScriptB I have an On$$anonymous$$ouseDown() function in which I am also calling functionA from ScriptA. In code it would look something like this.
using UnityEngine;
public class ScriptB : $$anonymous$$onoBehaviour
{
[SerializeField]
ScriptA SA;
void On$$anonymous$$ouseDown()
{
SA.functionA();
}
}
The error message of the original post is displayed next to the "ScriptA SA;" part. Like I said the script doesn't seem to be affected by this at all since functionA is called without any problems but I really would like to know why this warning appears and how to get rid of it.
Answer by dan_wipf · Sep 17, 2018 at 01:12 PM
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!
Your answer
Follow this Question
Related Questions
Visual Studio 2019 stopped working with Unity 2019.1.10f1 0 Answers
Error : GPGSUpgrader.cs(194,80): warning CS0162: Unreachable code detected 0 Answers
Visual Studio Code doesn't work for with unity 2 Answers
Is there any way to ignore all errors in a file? 1 Answer
Visual studio says no issues found even though there are errors. 1 Answer