I need some help on NullRefenceException Error

Hello, I’m using untiy since it name was Unity3d but my brain merge into itself here on this simple problem
thanks in advance

using UnityEngine;
using UnityEngine.UI;
public class Chacarter_controller : MonoBehaviour
{
    public float RotationX , RotationY , MinAngle, MaxAngle;
    public GameObject DuckMode_Col, NormalMode_Col;
    public Transform DuckMode_Transform, NormalMode_Transform;
    public Camera PlayerCam;
    public Rigidbody Rigid_Player;
    public KeyCode  Jump_key, Duck_key, Sprint_Key, Melee_key, Use_key;
    public float Sensitivity, SprintSpeed, SprintMultiplier , SprintSpeedMax , PlayerMain_Speed, DuckMain_Speed, Jump_Power, OriginToFeet_Distance, HeadClearDistance;
    public GameObject Active_Col;
    public bool IsTochingGround, AboveClear;
    public bool Ducking , MeleeCooling;
    public float MeleeCooldown = 10, MeleeDamage, MeleeThrowForce , MeleeDistance, meleeSpeedMultiplier;
    public Slider MeleeSlide;
    public float MaxEquipDist , DistanceFromTarget;
    public GameObject RayObject;
    public GameObject EquipSign;
    public float NonEquippedGunDist;
    public Transform HoldPosition;
    //Non-Public
    RaycastHit AttackedRigidMelee;
    Vector3 Move_Direction;
    RaycastHit GunRayHit;
    void Start()
    {
        Rigid_Player = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        GunSystem();
        Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out AttackedRigidMelee);
        if (AttackedRigidMelee.rigidbody != null)
        {
            DistanceFromTarget = (AttackedRigidMelee.transform.position - transform.position).magnitude;
        }else
        {
            DistanceFromTarget = 0;
        }

        if (MeleeCooldown < 0)
        {
            MeleeCooldown = 0;
        }
        MeleeSlide.value = MeleeCooldown / 10;

        if (Input.GetMouseButton(1))
        {
            Cursor.visible = false;
        }
        if (MeleeCooldown == 10 && MeleeCooldown > 0)
        {
                if (MeleeCooldown == 10 && Input.GetKeyDown(Melee_key) && AttackedRigidMelee.transform.gameObject != null && (AttackedRigidMelee.transform.position - transform.position).magnitude < MeleeDistance )

            {
                MeleeCooling = true;
                AttackedRigidMelee.rigidbody.AddForceAtPosition(PlayerCam.transform.forward * MeleeThrowForce , AttackedRigidMelee.point);
            }
        }
        if (MeleeCooldown == 0 && MeleeCooldown <= 10
            )
        {
            MeleeCooling = false;
          
        }
       if (!Ducking && Input.GetKey(Sprint_Key))
        {
            SprintSpeed = Mathf.Lerp(SprintSpeed, SprintSpeedMax, SprintMultiplier * Time.deltaTime);
        }
       else
        {
            SprintSpeed = 1;
        }
        if (Input.GetKey(Duck_key))
        { Ducking = true; }
        else if (AboveClear)
        { Ducking = false; }

        if (Ducking)
        {
            PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position , DuckMode_Transform.position, DuckMain_Speed* Time.deltaTime);
            DuckMode_Col.SetActive(true);
            NormalMode_Col.SetActive(false);
            Active_Col = DuckMode_Col;
        }
        else
        {
            PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position, NormalMode_Transform.position, DuckMain_Speed * Time.deltaTime);
            DuckMode_Col.SetActive(false);
            NormalMode_Col.SetActive(true);
            Active_Col = NormalMode_Col;
        }

        //Rigid_Player.transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * Sensitivity);
        RotationY += Input.GetAxisRaw("Mouse X") * Sensitivity;
        RotationX += Input.GetAxisRaw("Mouse Y") * Sensitivity;
        RotationX = Mathf.Clamp(RotationX, MinAngle, MaxAngle);
        Rigid_Player.transform.eulerAngles = new Vector3(0,RotationY,0);
        PlayerCam.transform.eulerAngles = new Vector3(-RotationX, RotationY, 0);



        float HorizontalMovement = Input.GetAxisRaw("Horizontal");
        float VerticalMovement = Input.GetAxisRaw("Vertical");
        Move_Direction = (HorizontalMovement * transform.right + VerticalMovement * transform.forward).normalized;
   
        if (IsTochingGround && Input.GetKeyDown(Jump_key) && AboveClear)
        {
            Rigid_Player.AddForce(Vector3.up * Jump_Power);
        }
    }
    void OnCollisionStay(Collision collision)
    {
        if (Physics.Raycast(transform.position, Vector3.up, HeadClearDistance))
        {
            AboveClear = false;
        }
        else
        {
            AboveClear = true;
        }


        if (Physics.Raycast(transform.position, Vector3.down, OriginToFeet_Distance))
        {
            IsTochingGround = true;
        }
        else
        {
            IsTochingGround = false;
        }
    }
  
 
    void FixedUpdate()
    {
        if (MeleeCooling)
        {
            MeleeCooldown--;
        }
        else if (MeleeCooldown < 10)
        {
            MeleeCooldown++;
        }
        Walking();
    }
        public void Walking()
    {
        Vector3 FixForY_Axis = new Vector3(0, Rigid_Player.velocity.y, 0);
    
        Rigid_Player.velocity = Move_Direction * PlayerMain_Speed* SprintSpeed * Time.deltaTime;
        Rigid_Player.velocity += FixForY_Axis;
    }
    void GunSystem()
    {
     
        Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out GunRayHit);
        if (GunRayHit.rigidbody != null && GunRayHit.transform.GetComponent<GunMain>() != null)
        {
         NonEquippedGunDist = (transform.position - GunRayHit.transform.position).magnitude;
        }
        if(NonEquippedGunDist <= MaxEquipDist  && GunRayHit.rigidbody != null )
        {
            if (GunRayHit.transform.gameObject.GetComponent<GunMain>().IsEquipped == false && GunRayHit.transform.gameObject.tag == "Gun")
            {
                EquipSign.SetActive(true);
                if (Input.GetKeyDown(Use_key))
                    GunRayHit.transform.GetComponent<GunMain>().IsEquipped = true;
                GunRayHit.transform.GetComponent<GunMain>().EquippedPlayer = gameObject;
            }
        }
        else
        {
            EquipSign.SetActive(false);
        }
    }
}

on the 169th Line i get some “NullReferenceException:
Object reference not set to an instance of an object” error. Can you guys help me about it. :slight_smile:

Looks like you forgot to add reference to EquipSign in the inspector window.

i will lokk into it in a bit thanks for answer

Nope, İt was Added to reference

If the error says it’s in line 169, the only object you have in that line is EquipSign, so it must be null.
You can put just before that line an if (EquipSign == null) {Debug.LogWarnning (“EquipSign is null”); Debug.Break(); }
Since Break() will pause the game, you can check in the inspector if you’ve lost the reference at some point or what.
Are you sure that when you double click on the error it brings you to that line (EquipSign.SetActive(true) ; ?

  • Are you sure that the 169th line here is the same 169th line in your live script? So the error you’re reporting is on the same line we see here?
  • If so, are you sure you don’t set the EquipSign variable anywhere else in other scripts? Since you use public variables, it’s easily can changed from other places. Strongly recommend the usage of [SerializeField] private ; usage instead. So you can set this in the inspector but you cannot change it from other scripts by accident.
1 Like

Yes! you are right. I put “if (GunRayHit.transform != null )” before 167th line and it worked like a charm :slight_smile:

It was correct line i just forgot to add the condition that it wont be null :slight_smile: it was a bit late in here and i was sleepy you know codin is not working well with sleep :stuck_out_tongue:

Thank you guys for your advices. I’m sorry for taking your time.

Cool, glad that it worked.

Again, Thank you very much for solution