Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question warning CS0108: 'GrapplingGun.camera' hides inherited member 'Component.camera'. Use the new keyword

Discussion in 'Scripting' started by JimJPlaysYT, Dec 8, 2021.

  1. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    68
    I am confused about this
    My script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GrapplingGun : MonoBehaviour {
    4.  
    5.     private LineRenderer lr;
    6.     private Vector3 grapplePoint;
    7.     public LayerMask whatIsGrappleable;
    8.     public Transform gunTip, camera, player;
    9.     private float maxDistance = 100f;
    10.     private SpringJoint joint;
    11.     public AudioSource ggunsound;
    12.  
    13.     void Awake() {
    14.         lr = GetComponent<LineRenderer>();
    15.     }
    16.  
    17.     void Update() {
    18.         if(Input.GetMouseButtonDown(1)) {
    19.             StartGrapple();
    20.             Debug.Log("grapplinggun");
    21.             ggunsound.Play();
    22.         }
    23.         else if(Input.GetMouseButtonUp(1)) {
    24.             StopGrapple();
    25.         }
    26.     }
    27.  
    28.     //Called after Update
    29.     void LateUpdate() {
    30.         DrawRope();
    31.     }
    32.  
    33.     /// <summary>
    34.     /// Call whenever we want to start a grapple
    35.     /// </summary>
    36.     void StartGrapple() {
    37.         RaycastHit hit;
    38.         if(Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable)) {
    39.             grapplePoint = hit.point;
    40.             joint = player.gameObject.AddComponent<SpringJoint>();
    41.             joint.autoConfigureConnectedAnchor = false;
    42.             joint.connectedAnchor = grapplePoint;
    43.  
    44.             float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);
    45.  
    46.             //The distance grapple will try to keep from grapple point.
    47.             joint.maxDistance = distanceFromPoint * 0.8f;
    48.             joint.minDistance = distanceFromPoint * 0.25f;
    49.  
    50.             //Adjust these values to fit your game.
    51.             joint.spring = 4.5f;
    52.             joint.damper = 7f;
    53.             joint.massScale = 4.5f;
    54.  
    55.             lr.positionCount = 2;
    56.             currentGrapplePosition = gunTip.position;
    57.         }
    58.     }
    59.  
    60.  
    61.     /// <summary>
    62.     /// Call whenever we want to stop a grapple
    63.     /// </summary>
    64.     void StopGrapple() {
    65.         lr.positionCount = 0;
    66.         Destroy(joint);
    67.     }
    68.  
    69.     private Vector3 currentGrapplePosition;
    70.    
    71.     void DrawRope() {
    72.         //If not grappling, don't draw rope
    73.         if(!joint) return;
    74.  
    75.         currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 8f);
    76.        
    77.         lr.SetPosition(0, gunTip.position);
    78.         lr.SetPosition(1, currentGrapplePosition);
    79.     }
    80.  
    81.     public bool IsGrappling() {
    82.         return joint != null;
    83.     }
    84.  
    85.     public Vector3 GetGrapplePoint() {
    86.         return grapplePoint;
    87.     }
    88. }
    89.  
    The error: upload_2021-12-8_20-17-30.png
    sorry it's small
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Since your GrapplingGun class inherits from MonoBehavior, it already has a member called "camera" in its base class. This is pretty much just a leftover from when Unity provided all these shortcuts to common components you might have attached to your objects, and the proper way to get references to other components now is with GetComponent<>.

    In any case, because that member is still there, if you want your own variable called "camera", you'll have to preface it with "new" as the error message suggests, and that member will now hide the reference inherited from the base class.
    Code (CSharp):
    1. public new Transform camera;
    That's what she he said. :cool:
     
  3. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    68
    ok thanks

    Very funny:rolleyes:
     
  4. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    68
    All fixed:)
     
  5. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    NOO It's ALWAYS SHE
     
  6. boxten97

    boxten97

    Joined:
    Apr 18, 2022
    Posts:
    1

    can u plz tell me where should i put this line....i am first time using unity
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You'd place it inside the script where you're trying to use a property that hides an inherited property. It's very possible you don't need to do this, though. One thing you'll want to make sure of when starting out is to not just copy code you find online, and instead make sure you're writing it all yourself and understanding why you're adding each line you're adding.

    If you have a question of your own, better to start a new thread and provide more details so you can get a more personalized response.