Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by stingman · Dec 22, 2012 at 06:58 AM · c#errorvariableused

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!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · 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 stingman · Dec 22, 2012 at 07:31 AM 0
Share

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

avatar image Octopoid stingman · Aug 02, 2016 at 11:20 PM 0
Share

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.

avatar image Bunny83 Octopoid · Aug 02, 2016 at 11:59 PM 1
Share

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)

avatar image
4
Wiki

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.

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
avatar image
3

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
 

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 Bunny83 · Aug 02, 2016 at 11:50 PM 0
Share

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.

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Can't import a variable from another script!! 1 Answer

error CS0841: A local variable `holdSound' cannot be used before it is declared 1 Answer

Problem calling variable across scripts (Closed) 1 Answer

Script not working, help please 1 Answer

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges