Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

The new keyword is not required?

Discussion in 'Scripting' started by Nigey, Jun 22, 2018.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I'm getting loads of warnings in my code: warning CS0109: The member `ElectronicDevice.audio' does not hide an inherited member. The new keyword is not required. These are dotted all over my project. Now I could get rid of these by renaming the variables, but I'd prefer to not need to dodge Unity's legacy stuff. Usually I use the 'new' keyword to hide inherited members, but now the new keyword is throwing this error up instead. It looks like #pragma warning disable 0109 isn't even doing the trick.

    Any thoughts?

    Cheers
     
    KpyaccaH likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Post code.
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Example:

    Code (CSharp):
    1.     [RequireComponent(typeof(AudioSource))]
    2.     public abstract class ElectronicDevice : BaseHazmat
    3.     {
    4.         public enum ElectronicState { Transitioning, Off, On }
    5.    
    6.         [SerializeField] private IInputtableContainer inputListener;
    7.         [SerializeField, ReadOnly] private ElectronicState electronicState = ElectronicState.Off;
    8.    
    9.         [SerializeField] private float displayRefreshRate = 0.25f;
    10.         [SerializeField] private float powerSavingTimer = 8f;
    11.         [SerializeField] private bool startTurnedOn = true;
    12.         [SerializeField] private bool endPowerSavingOnInput = true;
    13.  
    14. #pragma warning disable 0109
    15.         private new AudioSource audio;
    16. #pragma warning restore 0109
    17.         private bool powerSavingMode = false;
    Assets/Scripts/framework/Gameplay/Tools/ElectronicDevice.cs(19,33): warning CS0109: The member `Hazmat.ElectronicDevice.audio' does not hide an inherited member. The new keyword is not required.

    It's all the rigidbody, renderer, ect ones too.
     
    Last edited: Jun 22, 2018
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    If the base class does not have a variable called audio then you don't need the new keyword to declare it.
     
    xVergilx and lordofduct like this.
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Yeah, but HazmatBase is extending MonoBehaviour. It happens here too:

    Code (CSharp):
    1. public class Door : MonoBehaviour
    2. {
    3.     [SerializeField] private float OpenSpeed = 1;
    4.     [SerializeField] private float CloseSpeed = 1;
    5.     [SerializeField] private bool isAutomatic = false;
    6.     [SerializeField] private bool AutoClose = false;
    7.     [SerializeField] private bool DoubleSidesOpen = false;
    8.     [SerializeField] private string PlayerHeadTag = "MainCamera";
    9.     [SerializeField] private string OpenForwardAnimName = "Door_anim";
    10.     [SerializeField] private string OpenBackwardAnimName = "DoorBack_anim";
    11.  
    12.     private new Animation animation;
    13.     private Vector3 relativePos;
    14.     private string animName;
    15.     private bool isOpen = false;
    16.  
    Assets/Environments/office/QA_Office/Scripts/Door.cs(15,27): warning CS0109: The member `Door.animation' does not hide an inherited member. The new keyword is not required.

    It's a bit of a weird one. Using Unity 2018.1.0f2
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Bunny83 likes this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Yeah, because there is no 'animation' property/field either on MonoBehaviour.

    Unity removed all those old shortcut properties found in earlier versions of unity. They were first deprecated in Unity 5, and now removed in Unity 2018.

    Check here, no 'animation', no 'audio':
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
     
    Bunny83, xVergilx and Nigey like this.
  8. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Strange, any ideas why I'm getting this?

    Code (CSharp):
    1. public class Door : MonoBehaviour
    2. {
    3.     [SerializeField] private float OpenSpeed = 1;
    4.     [SerializeField] private float CloseSpeed = 1;
    5.     [SerializeField] private bool isAutomatic = false;
    6.     [SerializeField] private bool AutoClose = false;
    7.     [SerializeField] private bool DoubleSidesOpen = false;
    8.     [SerializeField] private string PlayerHeadTag = "MainCamera";
    9.     [SerializeField] private string OpenForwardAnimName = "Door_anim";
    10.     [SerializeField] private string OpenBackwardAnimName = "DoorBack_anim";
    11.  
    12.     private Animation animation;
    13.     private Vector3 relativePos;
    14.     private string animName;
    Assets/Environments/office/QA_Office/Scripts/Door.cs(15,23): warning CS0108: `Door.animation' hides inherited member `UnityEngine.Component.animation'. Use the new keyword if hiding was intended

    Assets/Environments/office/QA_Office/Scripts/Door.cs(15,27): warning CS0109: The member `Door.animation' does not hide an inherited member. The new keyword is not required


    It now shows both warnings simultaneously?! Lol. Plus this:

     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    That's weird......

    I don't have 2018 installed, I'll check when I get a chance.

    [edit]
    Like I said, I'll have to check. But I'm betting that the property is actually still defined, Unity has it flagged as obsolete/deprecated. And VisualStudio is just not understanding the 2 together... it sees it as a member, but not as hidable, for some nebulous reason.

    [edit2]
    In 2017.4, the property is still defined but marked Obsolete. I have to mark it new, but it doesn't say new isn't necessary.
     
    Last edited: Jun 22, 2018
    Nigey likes this.
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    There's a bunch of obsolete stuff in Component and Monobehavior that they don't want to remove. Just avoid using those names and choose something more descriptive.
     
    xVergilx and Nigey like this.
  11. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Thanks so much for checking that stuff out for me guys. It looks like I'll need to stomach it until 2018.2/3 (where I'd assume they perm remove them), or just go ahead and let the tail wag the dog and rename them lol.

    FYI - lord I am now drinking wine with a cat perusing Unity also.
     
    lordofduct likes this.
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Protip: don't wait on changes.
     
    xVergilx and Nigey like this.
  13. nglasl

    nglasl

    Joined:
    Oct 22, 2018
    Posts:
    7
    Just wanted to +1 to say that I hit this same thing today on 2018.3. It complains about the new keyword, so you remove it and then it complains that it needs the new keyword. In my case it was using:

    Code (CSharp):
    1. public new Camera camera;
     
    KpyaccaH, oli-2019 and QuasiStellar like this.
  14. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    another pro tip: Do not use the new keyword at all... because you will access the base field if you declare somewhere the base type as variable...
    Instead, use different names. e.g. use underscores in front of private members. And do not make any private member public. Use the [SerializeField] attribute instead. If you need public access, make a property for the field.
     
    dpgdemos likes this.
  15. bezier

    bezier

    Joined:
    Apr 1, 2014
    Posts:
    1
    For code that already existed, changing names might not be a good idea because you have to redo all related prefabs.
     
  16. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    You can use the attribute
    [FormerlySerializedAs("previousMemberName")]
    for this case. However, this will probably not work if your previous member name had the same name as a member of the base class and used the new-keyword (probably... I didn't try - maybe it works).
     
  17. arjhonfromph

    arjhonfromph

    Joined:
    Jul 29, 2022
    Posts:
    1
    i know its late i have the same problem to your issue, mine was (public ParticleSystem particleSystem;) to fix it i just added "new" like this (public new ParticleSystem particleSystem;)
     
  18. kwjsksai

    kwjsksai

    Joined:
    May 19, 2016
    Posts:
    1
    It says new isn't necessary when you actually build the project, but no warnings if you just play in editor.
     
  19. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Still a thing in 2021.3 They removed those properties from the build but not the editor assemblies, which is why the console spams the warning on build. Best you can do is...

    Code (CSharp):
    1. #pragma warning disable CS0109
    2.         public new Camera camera { get; private set; }
    3. #pragma warning restore CS0109
    And they still show up in auto-complete...

    upload_2023-3-23_9-18-24.png

    I was under the impression that things deprecated/marked obselete would typically be removed within the next 10 years. I understand the argument for backwards compatibility, but it's getting in the way of new development.
     
  20. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,092
    Is it really that restrictive to have to choose a name that isn't a lowercase version of the class name?
     
  21. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Sure I could call it
    gameCamera
    or
    currentCamera
    or
    cachedCamera
    , but that tells you little more than
    camera
    ; it's just more to type. I could call it
    theCameraThatThisComponentInitializesInOnEnableSoYouDontNeedToCallGetComponentEveryFrame
    , but that's a little bit long. I could call it
    cam
    , but that reminds me of my friend Cam with the stupid mustache.

    Having a usable, concise, API can be fairly important for certain use cases (eg I'm preparing a package right now to put on the asset store).
     
    Petethegoat and Ryiah like this.