GravityBody hides Rigidbody
Hello! I'm a tad new to Unity, and even newer to C#. I've been using Sebastian League's planetary gravity system to build a mini-game for fun. However, I've recently added in a new character model, and I've been getting the following error.
Assets/GravityBody.cs(8,19):warning CS0108: 'GravityBody.rigidbody' hides inherited member 'UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended.
Now, I'm well aware there are a few other topics related to this issue. But I'm assuming the answers for all of them don't really apply to this situation. I'd love to get some help here... By the way, here's some code.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class GravityBody : MonoBehaviour {
GravityAttractor planet;
Rigidbody rigidbody;
void Awake () {
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
rigidbody = GetComponent<Rigidbody> ();
// Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate () {
// Allow this body to be influenced by planet's gravity
planet.Attract(rigidbody);
}
}
Answer by M-Hanssen · May 10, 2016 at 09:16 AM
You cannot name your Rigidbody "rigidbody" because it hides the legacy "rigidbody" variable name.
Think of another name like "gravityRigidbody" ;-)
Or do the following to the variable declaration:
private new Rigidbody rigidbody;
Also, I do have one more issue, it's with the first person controller script. The console appears to be giving me this issue.
Assets/FirstPersonController.cs(20,19): warning CS0108:
FirstPersonController.rigidbody' hides inherited member
UnityEngine.Component.rigidbody'. Use the new keyword if hiding was intended.
Here's the code. using UnityEngine; using System.Collections; [RequireComponent (typeof (GravityBody))] public class FirstPersonController : $$anonymous$$onoBehaviour { // public vars public float mouseSensitivityX = 1; public float mouseSensitivityY = 1; public float walkSpeed = 6; public float jumpForce = 220; public Layer$$anonymous$$ask grounded$$anonymous$$ask; %|-367189528_8|% // System vars bool grounded; Vector3 moveAmount; Vector3 smooth$$anonymous$$oveVelocity; %|260141021_13|% Transform cameraTransform; Rigidbody rigidbody; void Awake() { Cursor.lockState = CursorLock$$anonymous$$ode.Locked; Cursor.visible = false; %|1409941851_21|% rigidbody = GetComponent<Rigidbody> (); } %|-1972943753_24|% void Update() { // Look rotation: transform.Rotate(Vector3.up * Input.GetAxis("$$anonymous$$ouse X") * mouseSensitivityX); verticalLookRotation += Input.GetAxis("$$anonymous$$ouse Y") * mouseSensitivityY; verticalLookRotation = $$anonymous$$athf.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; %|-691205467_38|% moveAmount = Vector3.SmoothDamp(moveAmount,target$$anonymous$$oveAmount,ref smooth$$anonymous$$oveVelocity,.15f); // Jump if (Input.GetButtonDown("Jump")) { if (grounded) { rigidbody.AddForce(transform.up * jumpForce); } } // Grounded check Ray ray = new Ray(transform.position, -transform.up); RaycastHit hit; if (Physics.Raycast(ray, out hit, 1 + .1f, grounded$$anonymous$$ask)) { grounded = true; } else { grounded = false; %|-1143165390_57|% } void FixedUpdate() { %|-654216127_62|% Vector3 local$$anonymous$$ove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime; rigidbody.$$anonymous$$ovePosition(rigidbody.position + local$$anonymous$$ove); } }
What the hell dude!? What part of my answer don't you understand??? The solution to this problem is exactly the same...
Change Rigidbody rigidbody;
to private new Rigidbody rigidbody;