Ok I now we should avoid Static Variables , But never got the reason behind this. Can anyone plzz explain me this?
And One more thing, i just came by a friend of mine who told me that He never uses
getComponent() to find scripts . He rather makes all classes Static and refers all the variables via ClassName.
I know hes doing it wrong but i cant exactly explain the loss of it .Plz help me out here in clearing few things of mine. So that i can help others.
Static variables belong to a class, rather than to an instance of the class.
So, they’re ok to use if you only ever have one of something in your game - such as a global score manager, and you’d be able to access any static variables in that class as e.g. ScoreManager.CurrentScore. (However, it’s not necessary to use static variables for this - it would be perfectly fine to have an instance of a score manager class and keep track of score in a member variable).
You should not use static variables to keep track of something that you might have more than one of - the health of an enemy, for example, or else all the enemies in your game will share the same health (because it belongs to the enemy class, not to each instance of the enemy class).
Sometimes people use static variables because they seem to think it’s “easier” to access those variables than to use GetComponent() to access the variables in a particular in a particular instance. This is a terrible idea, and will almost certainly lead to problems further down the line.