Search Unity

Question Cannot be accessed with an instance reference; qualify it with a type name instead?

Discussion in 'Scripting' started by YogaindoCR, Dec 4, 2022.

  1. YogaindoCR

    YogaindoCR

    Joined:
    Jul 29, 2021
    Posts:
    1
    Hello there I am a beginner user
    So I was trying to make a pickup code. without the "CurrentObject.constraints.FreezeRotation = false;"
    It was working as intended, but I want to lock the rotation physic object that has been picked up. but, I got this error instead: Member 'RigidbodyConstraints.FreezeRotation' cannot be accessed with an instance reference; qualify it with a type name instead. How can I fix this? Thank you

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine;
    4. namespace StarterAssets
    5.  
    6. {
    7. public class PickupScript : MonoBehaviour
    8. {
    9.     [SerializeField] private LayerMask PickupMask;
    10.     [SerializeField] private Camera PlayerCam;
    11.     [SerializeField] private Transform PickupTarget;
    12.     [Space]
    13.     [SerializeField] private float PickupRange;
    14.     private Rigidbody CurrentObject;
    15.  
    16.  
    17.         void Start()
    18.         {
    19.             animator = GetComponent<Animator>();
    20.         }
    21.     public void OnInteraction(InputValue value)
    22.     {
    23.         {
    24.             if(CurrentObject)
    25.             {
    26.                 CurrentObject.useGravity = true;
    27.                 CurrentObject = null;
    28.                 [I]CurrentObject.constraints.FreezeRotation = true;[/I]
    29.                 return;
    30.             }
    31.  
    32.             Ray CameraRay = PlayerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    33.             if (Physics.Raycast(CameraRay, out RaycastHit HitInfo, PickupRange, PickupMask))
    34.             {
    35.                 CurrentObject = HitInfo.rigidbody;
    36.                 CurrentObject.useGravity = false;
    37.                 [I]CurrentObject.constraints.FreezeRotation = true;[/I]
    38.                 }
    39.         }
    40.     }
    41.  
    42.     void FixedUpdate()
    43.     {
    44.         if(CurrentObject)
    45.         {
    46.             Vector3 DirectionToPoint = PickupTarget.position - CurrentObject.position;
    47.             float DistanceToPoint = DirectionToPoint.magnitude;
    48.             CurrentObject.velocity = DirectionToPoint * 12f * DistanceToPoint;
    49.         }
    50.     }
    51. }
    Console:
    PickupScript.cs(41,17): error CS0176: Member 'RigidbodyConstraints.FreezeRotation' cannot be accessed with an instance reference; qualify it with a type name instead
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    dadenguy2 likes this.