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): //In update int testVar = 0; testVar = 0; //Stupid error that it is never used *EDIT: Meanwhile this clears the error, is this how you script in C#? Is this normal? Code (CSharp): int testVar = 0; int howAboutThis = 0; testVar = 1; //Stupid error that it is never used howAboutThis = testVar; //Oh come on....
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.
You also don't need this: Code (CSharp): testVar = 0; or this: Code (CSharp): else { testVar = 0; } And you only need 2 different values on single variable, you should probably use a bool.
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.