- Home /
Unity 5.5.1 - Weird and conflicting variable never used warning.
I have the following in a script:
private Brick brick;
void Start()
{
brick = GameObject.FindObjectOfType<Brick>();
}
This is only a very small portion of the code in this script.
I keep getting the warning that the variable "brick" is assigned but never used. If i delete the variable, of course, I get an error that says "the name brick does not exist in the current context.
It can't be that I have assigned the variable and simultaneously not assigned it!
Anyone know the answer to this? it's a bit annoying. I'm close to finishing up my game and want to clear out all the warning but this one is stubborn.
Answer by nug700 · Mar 05, 2017 at 01:48 AM
You get the warning because, based on that code, even though your are assigning brick a value with "brick = GameObject.FindObjectOfType", you are not actually using it. Using it would be calling a method in Brick, getting data from a field/property, equating it to something, etc. This warning is only to notify you that the variable isn't used for anything, and could be removed.
Right, note that this warning only shows up for "private" variables. private variables can only be accessed from within the class and therefore the compiler can say for sure that a variable isn't used. If you make it public or protected the warning would disappear.
@jessesloan1: $$anonymous$$eep in $$anonymous$$d that the point of a variable is to store some value / reference for later use. Storing something in a variable and never doing anything with that stored something is pointless. So you have to ask yourself: Why do you actually store that Brick reference in that variable? Either your code is not finished, so the actual use of this variable hasn't been added yet, or it's indeed pointless and the variable as well as the assignment can be removed completely.
Didn't think of the private vs public thing. $$anonymous$$akes complete sense, though, so someone doesn't get a million of these errors when making an API or library.
Answer by jessesloan1 · Mar 05, 2017 at 04:18 AM
Yeah, this was a big "Derp!" moment for me today. I set that variable and used it and then decided to go back and take a different approach. I guess I forgot all about deleting all of it and then today I did not look any further before I posted this silly question. sigh....
Your answer
Follow this Question
Related Questions
Suddenly warnings are thrown as errors at random in Unity 1 Answer
implicit downcast 'Object' to "UnityEngine.Quaternion" 1 Answer
AssetBundle Manager - Update for Unity3D 5.4? 1 Answer
3dplatform tutorial compiler errors out of box -1 Answers
Scripts were perfect but now they are all messed up please help 0 Answers