Search Unity

Bug Obsolete Component members, CS0108, CS0109, and you:

Discussion in 'Scripting' started by nitz, Nov 18, 2021.

  1. nitz

    nitz

    Joined:
    May 4, 2011
    Posts:
    54
    Okay, Unity. I'm tired of you trying to have it both ways.

    In 2020.3.22f1 (Latest LTS as of writing), the class
    Component
    still has the long-since obsoleted variables for a handful of named components. That is, things like
    renderer
    ,
    rigidbody
    , et. al.

    As it is today, trying to reuse one of those names like so:

    Code (CSharp):
    1. [SerializeField]
    2. private Rigidbody rigidbody;
    Will have you met with CS0108:

    Assets\Scripts\Player.cs(22,21): warning CS0108: 'Player.rigidbody' hides inherited member 'Component.rigidbody'. Use the new keyword if hiding was intended.


    So you know the risks associated with hiding an inherited member, so you decide to push forward and do what the compiler instructs you to:

    Code (CSharp):
    1. [SerializeField]
    2. private new Rigidbody rigidbody;
    But then, lo and behold, the compiler is now upset, but differently:

    Assets\Scripts\Player.cs(22,25): warning CS0109: The member 'Player.rigidbody' does not hide an accessible member. The new keyword is not required.


    Well here in lies the rub: it should be quite impossible for both statements of the CS0108 and CS0109 to be true, yet here we are. As found in this thread,
    #pragma disable
    doesn't even get around the issue.

    The issue itself, around since 2015 (NEARLY 7 YEARS), was closed as "by design"! The members themselves still exist in the class today, to do nothing more than throw not supported exceptions. Who's palm do I need to grease to get `Component.deprecated.cs` finally deleted from the codebase?

    It's time, Unity. The members have been deprecated for close to the larger part of a decade, and you're still squatting on the names for no good reason at all. They are relics of a long gone age, and they should be removed once and for all.

    P.S.: To the people who suggest to pick different names: I'd like very much not to. Sometimes it makes sense to just call the camera,
    camera
    . And no, it doesn't make sense to say "just slap an underscore in front of it", so long as Rider ships with a standard rule for Unity serialized fields that says the suggested naming pattern is camelCase (not to mention trying to design classes that look and behave like built-in ones.)
    2021-11-18_14-14-54.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Agreed. But I hardly ever care, as this was so long ago deprecated... was it Unity 4 I think?

    For nearly a decade I have a habit of using alternate names:

    Common:

    rndrr
    - the renderer
    cam
    - the camera
    I hate typing out
    rigidbody
    too so that's always just
    rb
    or
    rb2d


    Less Common:

    azz
    - the AudioSource (can't use
    as
    or
    ass
    really)
    col
    - for collider (or
    col2d
    )

    This has the added benefit that if I'm grepping an entire filesystem (multiple solutions) looking for instances of where I used a renderer, I can search
    rndrr
    and only what I did comes up.
     
  3. nitz

    nitz

    Joined:
    May 4, 2011
    Posts:
    54
    This made me literally laugh out loud.

    And yeah, I can appreciate the ability to grep/ag/rg for something like `rndrr`, but that’s a little less useful for things like `col` (which could def be confused with a column, for example.)

    I think my largest frustration comes from the fact that they *were* deprecated so long ago. Why deprecate them but not remove them? I definitely understand them being left there for some time as they transitioned away from them (so the upgrader could auto-update and everything as well.) But at this point they are just a mild annoyance that causes me to write around them more often than I would have imagined.
     
    Kurt-Dekker likes this.