Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug You just gotta love Unity... -.-

Discussion in 'Scripting' started by Wolfram, Mar 28, 2024.

  1. Wolfram

    Wolfram

    Joined:
    Feb 16, 2010
    Posts:
    261
    It is how many years ago that all direct accessors (except gameObject, transform, and tag) have been removed from the Component class?

    Code:
    private new BoxCollider collider
    {
    ...


    Unity is like:
    [CS0109] The member 'UiTouchableButton.collider' does not hide an accessible member. The new keyword is not required.


    Changed code to:
    private BoxCollider collider
    {
    ...


    Unity is like:
    [CS0108] 'UiTouchableButton.collider' hides inherited member 'Component.collider'. Use the new keyword if hiding was intended.



    EDIT: I forgot to mention that I had this problem in 2021.3.7, NOT in the current 2022.3.19. So the issue seems to be fixed by now - sorry for the confusion.
     
    Last edited: Apr 2, 2024
    SisusCo, marcoantap and Nad_B like this.
  2. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Yeah it's weird. We're just used to not declare fields called collider, camera...etc

    I think I saw a discussion where they plan to finally remove them, probably in Unity 7+.
     
    Ryiah, SisusCo, Bunny83 and 1 other person like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,109
    Right. I don't quite understand some of the cargo cult. Yes, some features are kept and marked as obsolete but still work. They are to encourage people to change as soon as possible but still work. However the component shortcut accessors has been made unusable ages ago. So there's literally no benefit at all to carry them along as vestigial parts of an ancient time. Some things they got rid extremely quick, considering their frequent change of the network libraries and solutions, while others are dragged along for no reason.

    Most developers who use Unity now probably have never worked with a Unity version where they were still functional. So for them it always was just something weird that doesn't work and causes problems.
     
    Nad_B, Ryiah and SisusCo like this.
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,561
    Wait... what version is OP on?

    I don't get this when I use the 'new' operator like OP shows:
    I'm on 2022.3.18 (LTS)
     
    Wolfram and Ryiah like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,468
    Same. I do see CS0108 when I don't include the
    new
    operator both with VS and Rider. I'm on 2023.1.11.
     
  6. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,743
    Using it with "new" is perfectly fine. OP is probably annoyed by having to add that keyword.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,586
    Yep, those are all gone in versions after Unity 6, as are many many other obsolete methods/properties too. Glad these specific ones are gone, they were a poor idea which didn't scale well despite trying to serve as a nice accessibility feature. No idea where it all started and I've been working here for 11 years!

    Looking forward to things like trigger callbacks passing an arg named "collider" and not "collision" or being able to name locals as "collider". o_O
     
    LaneFox, SisusCo, Nad_B and 6 others like this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,109
    I tend to prefix method arguments with an "a". So hungarian style but not to indicate type information but just the fact that it's an argument. It solves a lot of common name collision issues and also helps distinguishing arguments from other variables. Though I do understand that most people don't like this. Personally I don't like code like

    Code (CSharp):
    1. void SomeMethod(int x)
    2. {
    3.     this.x = x;
    4. }
    where you have to use "this" to distinguish between the member variable and the argument variable. To me that's way to messy and reminds me way too much of javascript where this is kinda the norm ^^.
     
    MelvMay likes this.
  9. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,743
    Nice to hear that Unity will rejuvenate the API here and there together with CoreCLR even if that means some breaking changes.
    A good C# dev should not have much trouble migrating such things quickly where the compiler complains.
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,586
    Trying to avoid speaking for thousands of internal devs over the years but backwards compatibility was always important internally, the problem was that when something migrated to deprecated then to obsolete it was then left. I recall there being a procedure that it should be left for no more than 1 or 2 major releases after being made obsolete but that was a guide rather than a policed law so it resulted in a lot of obsolete things being left.

    There's definately a huge "tidy up" operation going on with a major part being focused on removing tech-debt and it's wonderful to see. Like a nice hot shower if you will.
     
    SisusCo, mopthrow, jGate99 and 5 others like this.
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,109
    The most important things in that case would be clear communication about those breaking changes so people don't update their current projects. The Unity Hub should have extra warning dialogs besides the normal upgrade warning to avoid careless upgrades which would result in a disaster.
     
    DragonCoder likes this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,586
    To be clear, I'm talking about obsolete not deprecated. Deprecated still work, obsolete don't or shouldn't. :)
     
  13. Wolfram

    Wolfram

    Joined:
    Feb 16, 2010
    Posts:
    261
    Very good point, sorry I forgot to mention the Unity version.
    The problem occurred in 2021.3, and it indeed seems to be fixed in 2022.3! \o/
     
  14. Wolfram

    Wolfram

    Joined:
    Feb 16, 2010
    Posts:
    261
    No, I was annoyed by a warning that was impossible to get rid of, no matter whether you used the "new" keyword or not. Read my original post again.

    But as others pointed out, that problem was apparently fixed in 2022.x/2022.3.
     
    Nad_B likes this.
  15. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    1,005
    My personal method to fix this has always been to take the microsoft approach. Class variables are Pascal case, locals are camel case.

    Code (CSharp):
    1. void SomeMethod(int x)
    2. {
    3.     X = x;
    4. }
     
    Ryiah, Spy-Master and Bunny83 like this.
  16. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,148
    You can perhaps use
    Code (csharp):
    1. #pragma warning disable IDE1006
    2. ...
    3. #pragma warning restore IDE1006
    Obviously replace the warning code with what you're getting
     
    Ryiah and Bunny83 like this.
  17. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,148
    And (edit: private) class members fields have a '_' prefix (in my case), so
    Code (csharp):
    1. float _val;
    2.  
    3. public void Assign(float val)
    4.   => _val = val;
    Another way when assigning a bunch of vars in ctors is to do a value tuple assignment.
    Code (csharp):
    1. float _x, _y, _z;
    2.  
    3. public MyVector(float x, float y, float z)
    4.   => (_x, _y, _z) = (x, y, z);
     
    Last edited: Apr 3, 2024
    Ryiah, Spy-Master and Bunny83 like this.
  18. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    1,005
    I see that a lot but personally never cared for it in that context. IDEs that are less than a quarter-of-a-century old can give me all of that context instantly. It's why I think the progenitors of Hungarian Notation themselves (Microsoft) stopped using it. Because all of that context is usually either color-coded or right at your fingertips anyway.

    Although - I do still use it from time to time specially when I'm declaring a backing field for an otherwise identically named property.

    Code (csharp):
    1.  
    2. public float MyValue => _MyValue;
    3. float _MyValue;
    4.  
     
    Ryiah, orionsyndrome and Bunny83 like this.
  19. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,148
    I agree it's vestigial, but there you go, sometimes you just wanna glance over it (or make names similar if they share context). I never do PascalCase with it though, that's kinda dirty lol. But I do like having some low-level internal functions be snake_cased, so there is a lot of room for personal quirkiness I guess :)
     
    Sluggy likes this.
  20. Wolfram

    Wolfram

    Joined:
    Feb 16, 2010
    Posts:
    261
    That is of course a valid suggestion - but IMHO it is never a clean solution to simply suppress warnings you don't want to see ;-)
    What I meant by "impossible to get rid of" is, no matter what you do to your code (except #pragma or refactoring your variable name), you will get either CS0109 or CS0108.
     
    orionsyndrome and Bunny83 like this.