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. Dismiss Notice

First person controller. rigidbody hides inherited member Error

Discussion in 'Getting Started' started by Vampyr_Engel, Feb 26, 2016.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Hi I am getting this 'First person controller. rigidbody hides inherited member UnityEngine.Component.rigidbody' Use the keyword if hiding was intended' message all of a sudden when my other scripts were working fine, What is wrong and how do I fix this?
     
    caspr_ likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    You left out the keyword it was telling you to use ('new'). That's important if you want to actually fix the problem. :)

    So, UnityEngine.Component (from which MonoBehaviour is derived, so your script is too) has a "rigidbody" member. Your script inherits that. It's not a useful member anymore, since all it does is cause the compiler to complain that this has been removed and offer to update your script, but it's there nonetheless.

    Now you've gone and created a "rigidbody" member in your script. So we have the same identifier defined in two places in the inheritance chain: in your script, and in Component. This is known as "hiding" (your property hides the inherited one). Property or method hiding makes C# feel all squirmy and uncomfortable, because it thinks it's likely to lead to errors. So it complains (emits a warning) about it.

    You can tell it to shut up and deal by simply adding the "new" keyword (as the error message says) in front of your own rigidbody property.
     
    Gametyme, bodeliang, EdwardJi and 2 others like this.
  3. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    [Q Thank you Joe so the (new) instructions are in the brackets? Thanks I will try them out and hopefully they work
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A better way would be to simply rename your member to something other then rigidbody.

    There is a reason the compiler gets iffy about hiding members. It can and does lead to unusual bugs.
     
  5. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Thank you I trie what you said and it worked however I am getting a "Can't add script because script class can't be found" error
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    It's always helpful to post the exact error you get. A paraphrasing of the error may easily be misunderstood.

    In this case, I think what you got is the error that occurs when the name of the script file doesn't match the name of the actual class. Please check that the class name (after "class" in the source code) matches the file name.
     
  7. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    using UnityEngine;
    using System.Collections;

    [RequireComponent (typeof (GravityBody))]
    public class FirstPersonHoverTank : MonoBehaviour {

    // public vars
    public float mouseSensitivityX = 250;
    public float mouseSensitivityY = 250;
    public float hoverSpeed = 20;
    public float jumpForce = 1000f;
    public float hoverForce = 65f;
    public float hoverHeight = 3.5f;
    public LayerMask groundedMask;

    // System vars
    bool grounded;
    Vector3 moveAmount;
    Vector3 smoothMoveVelocity;
    float verticalLookRotation;
    Transform cameraTransform;
    Rigidbody Hovertank ;




    void Awake() {
    Screen.lockCursor = true;
    cameraTransform = Camera.main.transform;
    Hovertank = GetComponent<Rigidbody> ();
    }

    void Update() {

    // Look rotation:
    transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
    cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;

    // Calculate movement:
    float inputX = Input.GetAxisRaw("Horizontal");
    float inputY = Input.GetAxisRaw("Vertical");

    Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    Vector3 targetMoveAmount = moveDir * hoverSpeed;
    moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);

    // Jump
    if (Input.GetButtonDown("Jump")) {
    if (grounded) {
    Hovertank.AddForce(transform.up * jumpForce);
    }
    }

    // Grounded check
    Ray ray = new Ray(transform.position, -transform.up);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    grounded = true;
    }
    else {
    grounded = false;
    }

    }

    void FixedUpdate() {
    // Apply movement to rigidbody
    Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    Hovertank.MovePosition(Hovertank.position + localMove);
    }
    }
     
    Last edited: Mar 8, 2016
  8. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    OK I hope that was OK and I will do what you suggest thank you
     
  9. Vyndian

    Vyndian

    Joined:
    Feb 25, 2020
    Posts:
    1
    This was super helpful! I got the error while running through the "Ruby's 2D Adventure" tutorial, and they don't address how to resolve it (I don't believe). They mention that your running over an old member we won't need, but the error is never mentioned.
    In the future, I will just use a different name. For the tutorial, however, I will just use the "new" keyword so my script matches the tutorial script.
    Thanks ya'll!
     
  10. ashlynrparsons

    ashlynrparsons

    Joined:
    Feb 14, 2020
    Posts:
    1
    wait im sorry where did you put the "new"? I'm having the same problem in the Ruby tutorial. its not breaking anything but its just annoying to look at lol
     
    AltheaF likes this.
  11. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    48
    You can't name your Rigidbody "rigidbody" because it hides the legacy "rigidbody" variable name.
    simply name it "rgdbody" or whatever.

    OR in the variable declaration:
    private new Rigidbody rigidbody;
     
  12. JudeIsong

    JudeIsong

    Joined:
    Dec 3, 2020
    Posts:
    9
    Please i a new here and working on a tutorial i came across online and i had same error. i am as new to c# as new to Unity 2D game development. this is y code and the error message

    "Assets\Scripts\TapController.cs(12,17): warning CS0108: 'TapController.rigidbody' hides inherited member 'Component.rigidbody'. Use the new keyword if hiding was intended."

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [RequireComponent(typeof(Rigidbody2D))]
    public class TapController : MonoBehaviour {

    public float tapForce = 10;
    public float tiltSmooth = 5;
    public Vector3 startPos;

    Rigidbody2D rigidbody;
    Quaternion downRotation;
    Quaternion forwardRotation;

    void Start(){

    rigidbody = GetComponent<Rigidbody2D>();
    downRotation = Quaternion.Euler(0,0,-90);
    forwardRotation = Quaternion.Euler(0,0,35);

    }

    void Update(){
    if (Input.GetMouseButtonDown(0)){
    transform.rotation = forwardRotation;
    rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
    }

    transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D col) {
    if (col.gameObject.tag == "ScoreZone"){
    //register a score event
    //play a sound
    }

    if (col.gameObject.tag == "DeadZone"){
    rigidbody.simulated = false;
    //register a dead event
    //play a sound


    }

    }

    }


    Please i need help soonest.

    Thanks anyone
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You know if you read mine and JoeStrouts posts up the chain you would have your solution.

    Either use 'new' to hide the variable, or change the name of the variable to something other than rigidbody.
     
  14. JudeIsong

    JudeIsong

    Joined:
    Dec 3, 2020
    Posts:
    9
    @Kiwasi: Please can you post an example on how to use the 'new' keyword to hide the variable? Thanks with expectation.
     
  15. JudeIsong

    JudeIsong

    Joined:
    Dec 3, 2020
    Posts:
    9
    @Kiwasi : Thanks a lot for your assistance. it worked.