Search Unity

Warning cs0108 (doubt)

Discussion in 'Editor & General Support' started by WagnerDav, Jul 7, 2020.

  1. WagnerDav

    WagnerDav

    Joined:
    Jul 6, 2020
    Posts:
    12
    So, I was reading an article from Unity itself:

    https://support.unity3d.com/hc/en-us/articles/206924676-What-is-CS0108-

    Because I was getting the warning:

    warning cs0108: 'Player.rigidbody' hides inherited member 'Component.rigidbody'. Use the new keyword if hiding was intended

    Because I had the following code:


    Code (CSharp):
    1.  
    2.     private Rigidbody2D rigidbody;
    3.  
    4.     private Animator animator;
    5.  
    6.     void Start()
    7.     {
    8.         rigidbody = GetComponent<Rigidbody2D>();
    9.         animator  = GetComponent<Animator>();
    10.  
    11.     }
    12.  
    So I changed how the article says and where Unity was accusing:

    Code (CSharp):
    1.  
    2.     private new Rigidbody2D rigidbody;
    3.     private Animator animator;
    4.  
    5.     void Start()
    6.     {
    7.         rigidbody = GetComponent<Rigidbody2D>();
    8.         animator  = GetComponent<Animator>();
    9.  
    10.     }
    11.  

    Since the article says "The CS0108 warning is displayed when a variable was declared with the same name as a variable in a base class, but does not use the new keyword.".
    But see an interesting thing happened, because Rigidbody2D needs the NEW operator but Animator doesn't?

    Again sorry for my english. Thanks!

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    WagnerDav likes this.
  3. WagnerDav

    WagnerDav

    Joined:
    Jul 6, 2020
    Posts:
    12
    Thank you! I'm starting with Unity, I don't know much about the library yet, I'm a little bit in the dark too because my VScode doesn't seem to want to pull the library properly. So I'm looking for readings and help in the forums.