Search Unity

[SOLVED] Unnecessary "The variable [...] is assigned but its value is never used"?

Discussion in 'Scripting' started by Shushustorm, Oct 8, 2015.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hello everyone!
    I get the
    "The variable [...] is assigned but its value is never used"
    warning in the console even though I pass its value to a function.
    I don't use it in any other way, though.
    Is this expected behaviour? Am I doing something wrong?

    Greetings,
    Shu
     
    sunniedeedee and hopetolive like this.
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    It'd be very useful if you posted the code, or at least that snippet here!
    Please use the code tags by clicking on the icon left to the flappy disk icon, then select "Code".

    This usually happens when you don't use the variable anywhere in the script. Even though you set a value into it, you need to use it in order to skip this warning. And note that it's just a warning, not any error, though I see why you want it to be gone (warnings can be very annoying). Therefore I'd appreciate if you posted the code here! :)
     
    hopetolive and Shushustorm like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The compiler is saying "you might have made a mistake here". It's common for me to get dozens of warnings in my code during early development. It's generally worth cleaning them up for production.
     
    Shushustorm likes this.
  4. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    This happens sometimes if you declare and fill a variable but only use it under a condition (commonly an if statement). It's harmless if you are sure that the code is working correctly otherwise. To remove the warning, I usually just pass the variable pointlessly into something that is currently not being processed outside of the conditions it is intended for, where it will safely be overwritten when the something is used for it's own purpose. Alternatively, you can just ignore the warning.
     
    Shushustorm likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sounds risky.
     
    Shushustorm likes this.
  6. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    That message in particular can be kind of a pain to get rid of, this is the fastest way I've found. It is risky, but if you're confident in the math, it's one way to do it. I try to stay away from conditional uses so this message simply doesn't come up.
     
    Shushustorm and Kiwasi like this.
  7. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Alright, I solved the problem. And it makes me want to facepalm.

    Simplified, this is what I wanted to do:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SomeSnippet : MonoBehaviour {
    5.  
    6.     string URLToApp = "0";
    7.  
    8.     void Start () {
    9.  
    10.     #if UNITY_IOS
    11.     string URLToApp = "https://...IOS";
    12.     #endif
    13.  
    14.     #if UNITY_ANDROID
    15.     string URLToApp = "https://...ANDROID";
    16.     #endif
    17.  
    18.     }
    19.  
    20.     void Update () {
    21.         if (Input.GetKeyDown("o")) {OpenURL(URLToApp);}
    22.     }
    23.  
    24.     void OpenURL (string URL_) {
    25.         Debug.Log(URL_);
    26.     //    Application.OpenURL(URL_);
    27.     }
    28.  
    29. }


    But, of course, I just didn't delete the "string"s after copy & paste:
    (The following code works nicely without any warnings whatsoever, so in this case, the warnings were actually a good thing.)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SomeSnippet : MonoBehaviour {
    5.  
    6.     string URLToApp = "0";
    7.  
    8.     void Start () {
    9.  
    10.     #if UNITY_IOS
    11.     URLToApp = "https://...IOS";
    12.     #endif
    13.  
    14.     #if UNITY_ANDROID
    15.     URLToApp = "https://...ANDROID";
    16.     #endif
    17.  
    18.     }
    19.  
    20.     void Update () {
    21.         if (Input.GetKeyDown("o")) {OpenURL(URLToApp);}
    22.     }
    23.  
    24.     void OpenURL (string URL_) {
    25.         Debug.Log(URL_);
    26.     //    Application.OpenURL(URL_);
    27.     }
    28.  
    29. }
    Thanks a lot for your replies, though!
     
    Kiwasi likes this.
  8. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    @Tiki what about
    #pragma warning disable/restore?
     
    Shushustorm and Tiki like this.
  9. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Good call, I hadn't even thought of it. Looks like #pragma warning disable 0219 removes this message in particular. Thanks Yash.
     
    Shushustorm likes this.
  10. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    You're Welcome ;)
     
  11. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Is there a way to use #pragma warning disable only in a certain area?
    Does is work like this?:
    Code (CSharp):
    1. // stuff that should get warnings
    2.  
    3. #pragma warning disable
    4. // stuff that should not get warnings
    5. #pragma warning restore
    6.  
    7. // stuff that should get warnings
     
  12. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Exactly and don't forget the error number....
    And one more thing. Using this a bad practice. Its is highly recommended to find the areas before finalizing your game. This is fine for debugging.
     
    Shushustorm likes this.
  13. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    @Yash987654321
    Thanks! So it is not possible to not use error numbers and suppress all the warnings that occur at once?
    This seems very handy when working with plugins from the Asset Store that show warnings.
    Because sometimes plugins will run fine, but they will show warnings all the time, which I don't know how to resolve.
     
  14. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    I don't know if we cannot disable all warnings at once (never tried) but if it do, its not a good idea to do it. And If you mean that some plugins show warnings... You cannot disable the warnings without having source code as warnings are disabled in same file only, but if it shows an warning while calling a method yourself it will surely help.
     
    Shushustorm likes this.
  15. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I see. Maybe I should try that when getting annoying warnings again.
    Most of the time when I use plugins, I can double-click the warnings in the console, which will open a script file that is located somewhere in the asset's folders. Then, I could try to look for the "error" myself, but I don't buy assets in order to bugfix. So if they work, I don't bother changing the code.
     
  16. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Yeah that warnings can be disabled.
    BTW if you want to disable all warnings just click the yellow button in the top right of the console. it will 'hide' the warnings from unity's console
     
    Shushustorm and Kiwasi like this.
  17. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks! And yes, I use the button most of the time, but on the other hand, I don't want to miss any important warnings. I just want to get rid of the warnings I won't bother to change anything about anyway. :D
     
    Yash987654321 likes this.
  18. Scorpion37s

    Scorpion37s

    Joined:
    May 31, 2021
    Posts:
    9
    I had a variable that is only used in an if statement that gets activated by the player, which I'm guessing is what Unity didn't like. What helped me was to just do some random stuff with it in the start method :)
     

    Attached Files: