Search Unity

Var assigned; Value not used

Discussion in 'Scripting' started by Khyrid, Jun 26, 2017.

  1. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    I'm trying to write a C# script and I keep getting an annoying warning that I assigned a var but never use it, but I do use it. Still the error persists.

    Code (CSharp):
    1.    
    2.         //In update  
    3.         int testVar = 0;
    4.         testVar = 0; //Stupid error that it is never used
    5.  


    *EDIT: Meanwhile this clears the error, is this how you script in C#? Is this normal?
    Code (CSharp):
    1.        int testVar = 0;
    2.         int howAboutThis = 0;
    3.         testVar = 1; //Stupid error that it is never used
    4.         howAboutThis = testVar; //Oh come on....
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    It's a warning, not an error.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It isn't used. What are you using it for? You assign a value to it, that's it. That's not being used. It's telling you exactly that. It's assigned but not used.

    And as @Marfab posted before I could finish, it's just a notice that you are creating a variable, assigning a value to it, but then not doing anything with it. It has no effect on your game.
     
    Khyrid likes this.
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    You also don't need this:
    Code (CSharp):
    1. testVar = 0;
    or this:
    Code (CSharp):
    1. else { testVar = 0; }
    And you only need 2 different values on single variable, you should probably use a bool.
     
  5. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Yes I see, unity script would never harass me about this, but I guess now that I'm using the big boy language, I have to start coding inside the lines.

    When I added "Debug.Log(testVar);" The error... I mean, the 'warning' went away because I gave the var purpose. I guess C# doesn't trust coders to remember their dead end vars. My work method has always been to set up the framework for what I need and then tie things together afterwards. I would leave a lot of unused vars sitting until I get around to tying them into something.

    I suppose however, this is a better practice and helps keep cleaner code.

    As for the else{testVar = 0;} etc, that was just troubleshooting for the warning I was getting as at the time I didn't understand why it saying it wasn't used.