• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MSachs · Sep 17, 2018 at 06:06 AM · errorerror messagevisual studiowarning

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MSachs · Sep 21, 2018 at 08:03 AM 0
Share

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 OnMouseDown() function in which I am also calling functionA from ScriptA. In code it would look something like this.

 using UnityEngine;
 
 public class ScriptB : MonoBehaviour
 {
     [SerializeField]
     ScriptA SA;
 
     void OnMouseDown()
     {
           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.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I dont know what this error is and it has no effect on the game at all 0 Answers

Visual Studio Code doesn't work for with unity 2 Answers

Error : GPGSUpgrader.cs(194,80): warning CS0162: Unreachable code detected 0 Answers

Visual Studio 2019 stopped working with Unity 2019.1.10f1 0 Answers

Script Error 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges