- Home /
C# - variable is assigned but its value is never used
Hey guys,
I converted a script from Javascript to C# but there are a few warnings I now get. I needed to create a temporary variable to convert certain lines of JS to C# and once I do this it clears up the error but I get a new warning. Can someone point me in the right direction to fix this?
Here was the Javascript code:
guiTextureForFadeIn.color.a = alphaInitial + ((alphaAfterFadeIn - alphaInitial) * timePercent);
Obviously I couldn't convert it like that to C# so I re-wrote it like so:
float alphaFadeIn = guiTextureForFadeIn.color.a;
alphaFadeIn = alphaInitial + ((alphaAfterFadeIn - alphaInitial) * timePercent);
And I'm getting the warning: "The variable 'alphaFadeIn' is assigned but its value is never used.'
How do I write it so this warning goes away?
Thank you!
Answer by DayyanSisson · Dec 22, 2012 at 07:11 AM
You're probably not using the variable anywhere in your script. You tell the script what alphaFadeIn is but you don't use it. If you just delete the variable the warning will go away.
yup I wasn't using the variable anywhere. What I don't get is why JS doesn't give the same warning. It's quite a messy script I'm converting. Tons of useless code. Thanks, just deleted all those variables :)
The answer doesn't relate specifically to Unity, but JavaScript allows dynamic scripts to be executed, for example by using eval
. These can be executed even in local scope, meaning the interpreter can't be sure if a variable is required or not.
C# on the other hand does not allow for such things, so it can deter$$anonymous$$e more certainly whether variables are actually being used, hence it can (usually) accurately provides these warnings.
That isn't much related to the fact that Javascript allows dynamic variables. First of all Unity doesn't use javascript but UnityScript which is a completely different language. Next thing is most UnityScript scripts have "#pragma strict" on the top so dynamic typing is disabled. And finally the UnityScript compiler throws a similar warning if you declare a private variable which you don't use:
BCW0014: WARNING: Private field 'USTest.test' is never used
The real problem is that the "converted" code doesn't do the same as the original code. The converted code should look like this:
// C#
Color tmp = guiTextureForFadeIn.color;
tmp.a = alphaInitial + ((alphaAfterFadeIn - alphaInitial) * timePercent);
guiTextureForFadeIn.color = tmp;
In which case the temp variable is used (and is required)
Answer by shigidaMark · Dec 22, 2012 at 07:30 AM
Add this to the beginning of your script to temporarily disable the message.
#pragma warning disable 0414
Be sure to remove it before your production version of the game, and if you are still receiving the message, then remove the line of code where the variable is declared, as you obviously don't still need the variable if it is still unused in production.
Answer by colinday · Aug 02, 2016 at 11:05 PM
There are valid reasons to have a variable that is assigned a value that is not used where you need to keep the variable in the final code.
One such example is when integrating with Valve's Steam platform via Steamworks.Net. When registering callbacks for Steamworks, it is necessary to store the result in a variable so that it doesn't get garbage collected according to the documentation at: https://steamworks.github.io/gettingstarted/#steam-callbacks
So, if you'd like to silence the warning just for the variables in question you can do this.
#pragma warning disable 0414 // suppress value not used warning
private int m_someAssignedToButNeverUsedVariable;
#pragma warning restore 0414 // restore value not used warning
You will never get that warning on protected or public variables. Only private variables will cause that warning as private is the only visibility where the compiler can be sure that a variable isn't used anywhere. Disabling warnings really isn't recommended. S$$anonymous$$m has a strange approach managing the callback this way.