Search Unity

Why is Static considered bad practice

Discussion in 'Scripting' started by mattSkr, Sep 13, 2013.

Thread Status:
Not open for further replies.
  1. mattSkr

    mattSkr

    Joined:
    Dec 23, 2012
    Posts:
    19
    I've been working with c# and unity for about a year now. I've read a lot of tutorials and q&a posts saying "Don't use static when referring to other scripts...".
    I've never seen an explanation to why static is bad practice. People just say it is and that's all.

    My reasoning, if I have a global variable/function that I may edit/call from multiple scripts, why not use it?
    Thanks for any insight or to a link that better educates me on this.

    Matt
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Everything has it's purpose. Statics are used by Unity. That's why you write Input.whatever. It's a static function of Input. If you only need one instance of something, you use a static. Maybe you're not using it enough. Maybe you should use static functions and static classes.
     
    Last edited: Sep 13, 2013
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You shouldn't use static variables as a "shortcut"...there's a tendency for newcomers to do things like make their enemy health a static variable, then they wonder why decreasing the health on one enemy changes them all. I'd say it's bad practice to use static if you don't fully understand exactly what it means and what the implications of using it are.

    --Eric
     
    DevAlone likes this.
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Generally static will hurt you the moment you start loading scenes, because when you use LoadLevel, static variables aren't cleared while non static variables are (in laymans terms). So Generally for less bugs you'll want to avoid static. Also static variables can be modified by anything else, which causes more potential bugs.

    I do use them, but only because a) I'm reasonably experienced b) I only use them for read only, not anything else. And frankly I can eliminate using them entirely if I choose. But I'm an old hand, I know what I'm doing and it's not collaborative. Once we add another programmer, it is unlikely I will use them at all.
     
  5. mattSkr

    mattSkr

    Joined:
    Dec 23, 2012
    Posts:
    19
    Ah, but lets say I have a gameobject that I have set not to destroy on loading a new scene and it holds, for example, playerTotalPoints.
    Knowing that it doesn't clear (which I don't want it to) between scenes, would this be an appropriate time to say static? Or did I just spout out a load of bad practice?
     
  6. jvil

    jvil

    Joined:
    Jul 13, 2012
    Posts:
    263
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yeah, I saw a little tutorial on some site where a guy used it for sort of a game manager. He kept all his information he wanted saved between scenes that way. These are games. You won't get arrested for playing around. If it doesn't work well, you will learn it first hand. Do some research on statics and then play around with them. They wouldn't be there if they weren't useful.
     
  8. mattSkr

    mattSkr

    Joined:
    Dec 23, 2012
    Posts:
    19
    Thank you all for the help. I really appreciate it.
    Links are helpful too and clarify a lot.
    I'm just at the point of my programming adventure where I want to start knowing better practices and have a better understanding of what's going on.
     
  9. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,261
    Static variables and functions are specifically used when there only needs to be a single instance of whatever it is you are referencing.

    You can use static as a shortcut/reference, but you have to be careful about where it is used. The Camera.mainCamera static reference is a shortcut to whatever camera is currently being used. This shortcut is acceptable because 90% of the time, only one camera is being used. When you are using multiple cameras for local split-screen, you can no longer use the Camera.mainCamera shortcut effectively.

    The important thing to remember is that a static variable exists as the same instance across all instances of a class. When you change a value on a static variable in one instance, it will change for every instance. Before using the static keyword make certain that this is something you want.

    Restrain yourself in using static variables. Think logically about when they would be appropriate. I normally only use them in rare cases, but I do use them.
     
    Last edited: Sep 14, 2013
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Only if they're public, but that applies to all public variables. Static is in addition to public/private/etc., not in place of. Just use getters/setters as normal.

    --Eric
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah, I just wanted to drum home the potential pitfalls rather than get into details - I figured anyone who knows static, will know this, and anyone who does not will be alerted.
     
Thread Status:
Not open for further replies.