Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Feature request: Have unity check for static variables.

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

  1. SupremeEnchanter

    SupremeEnchanter

    Joined:
    Nov 29, 2012
    Posts:
    184
    Using static variables can cause issues. if you have a script that is reused with a static variable, unity messes this up. So why can't unity check the scripts with the same name for the same static variables? Just give a warning in the console? Seems easy enough.

    Or have a button to press and unity would go through all of the scenes looking at the static variables and see if any are the same. Seems easy enough.

    Anyone? Bueller?
     
  2. dgoyette

    dgoyette

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

    SupremeEnchanter

    Joined:
    Nov 29, 2012
    Posts:
    184
    -Script "one" has static variable "whatever"
    -Use this script in a scene on multiple game objects.
    - Use the script in other scenes on other game objects.

    Unity will sometimes give the incorrect values for it. Probably as if confuses what is what. Obviously. don't use static variables this way would be ideal. However, why can't unity just look in the scenes and let you know you are reusing the same static variable? Seems easy enough.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    692
    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.
  5. SupremeEnchanter

    SupremeEnchanter

    Joined:
    Nov 29, 2012
    Posts:
    184
    I get it haley. But if using it wrong leads to problems, then why can't they just do a check when building if this might happen? This would be a console warning. Seems like the few minutes to add the code would save a lot of people a lot of time.
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    692
    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.
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,714
    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.
  8. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,661
    Also, Unity doesn't have anything to do with static variables. It a C# language feature.
     
  9. SupremeEnchanter

    SupremeEnchanter

    Joined:
    Nov 29, 2012
    Posts:
    184
    Oh my Gods people, why is it every time a person has a suggestion here everyone finds a million reasons why it can't be done. I should have made a disclaimer at the start about that. My bad. As my old Kung Fu teacher once told me: don't bring me problems, bring me solutions. So I have.

    halley...See disclaimer above.


    dgotte...If you have multiple scripts declaring the same static variable you will get errors in calculations. I've experienced this a few times and it sucks until you figure it out. So yeah, it's bad programming but to avoid hours and hours of issues, then why can' t there just be a simple check?

    xvergile...But unity compiles c#, makes a build and runs it. So when making a build why can't it just have a simple check to avoid problems. It's like when I mentioned that the UWP to Microsoft store only allowed a max size for the icons. HOWEVER, when making the PNG in photoshop it makes it larger than allowed. Unity allows this and gives no errors. This is not a unity thing. But they just fixed this (not sure if it's in 2019.1.3 or not). But because it cause a automatic fail on the Microsoft store, then why not have a check for the file size of an icon? Simple enough.
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,661
    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
  11. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,714
    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.