Search Unity

Feature request: Have unity check for static variables.

Discussion in 'Editor & General Support' started by RandomCharacters, May 17, 2019.

  1. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    .
     
    Last edited: May 21, 2020
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Can you give an example of what you mean? What is a "reused" static variable, and how does Unity mess it up?
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    The whole point to a static variable is that you reuse the variable values across different situations in both scope and lifetime. Unity cannot determine if you're using the feature in an unintentional way.
     
    xVergilx likes this.
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Warnings should not be used for things that are fundamental features working as they were intended. The number of scripts in Unity Asset Store which use static variables for appropriate purposes is mind bogglingly large. You would end up getting all new warnings every time you bought a new asset. It's like saying there should be a warning on straws because the interior of the tube might get wet.

    If you want to run a scan through your source code, finding everywhere that some code uses the static keyword, go right ahead. You can even make your own precommit audits on version control, or enforce some kind of policy about this, all outside Unity.

    But better yet, learn what static IS, and what static DOES, so you don't use it incorrectly in the future.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I'm not sure what you mean by this. In what way does Unity give incorrect values for it?

    Also, the fact that you refer to it as "values" is concerning. A static variable only has one value, ever. There's no such thing as "reusing" the static variable. Think of static variables as a single slot in memory that is shared by everyone. There's only room for one value in it.

    It kind of sounds like you misunderstood what static meant, and then had a confusing bug because of it. That doesn't mean that using static, in general, is problematic.
     
    xVergilx likes this.
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Also, Unity doesn't have anything to do with static variables. It a C# language feature.
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Unity uses Roslyn compiler with > .Net 4.5x, which isn't that simple to modify. Unless you're from MS team.
    There's like a warning issue with 0649 outputting to the console messages which defy Unity's [SerializeField] logic.
    And it haven't been fixed yet, because C# compiler doesn't support overriding messages (and adding custom logic to the compile pipeline) from the box.

    So either all that native code is supported and tested by MS default compiler, or pile of obsolete garbage that Mono was / is. Pick your poison.

    Also, feel free to make your own language, if you feel like C# doesn't fit your needs.
    Not everyone needs extra useless checks to slow down their compilation pipeline.
    Which is already slow even with Asmdefs.

    TL;DR: Its your own fault that you do not understand how C# language works. Get over it.
    Unity's just a compilation of useful libraries and 3rd party tools.
     
    Last edited: May 20, 2019
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I think part of the reason you're feeling attacked is because the things you're saying demonstrate a lack of some basic understanding of how C# works. No offense meant by that, but it sounds like you're still learning, and getting frustrated when things go wrong. There are plenty of ways you can shoot yourself in the foot with most any programming language. And Visual Studio is often extremely good at noticing when you're doing someone risky, and providing warnings in that case. This just isn't one of those times.

    To the specific thing you've said, "multiple scripts declaring the same static variable", that statement is unfortunately nonsense in C#. "Declaring" a variable means defining it like this:
    Code (CSharp):
    1. public class ClassA {
    2.     public static int MyStaticVal;
    3. }
    It's totally reasonable for multiple classes to define public variables that have the same name. For example:
    Code (CSharp):
    1. public class ClassA {
    2.     public static int MyStaticVal;
    3. }
    4.  
    5. public class ClassB {
    6.     public static int MyStaticVal;
    7. }
    Understand that this does not means that multiple classes have defined the same variable. ClassA.MyStaticVal is completely different from ClassB.MyStaticVal, and neither Visual Studio not Unity will ever mistake the two. You might mistake the two, and pick the wrong one, but that can be solved by naming your variables more clearly.

    Please also note that you've shown no code at all so far. I've asked for an example of the case you're talking about, where Unity messed up, or where something "lead to problems", but you're only talking about things very generally and (sorry) incorrectly. So, to get this conversation back on track, please show a very simple example of code you feel causes Unity to mess up.