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

I need some help on NullRefenceException Error

Discussion in 'Scripting' started by mero6191, Jun 24, 2019.

  1. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    Hello, I'm using untiy since it name was Unity3d but my brain merge into itself here on this simple problem
    thanks in advance

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class Chacarter_controller : MonoBehaviour
    4. {
    5.     public float RotationX , RotationY , MinAngle, MaxAngle;
    6.     public GameObject DuckMode_Col, NormalMode_Col;
    7.     public Transform DuckMode_Transform, NormalMode_Transform;
    8.     public Camera PlayerCam;
    9.     public Rigidbody Rigid_Player;
    10.     public KeyCode  Jump_key, Duck_key, Sprint_Key, Melee_key, Use_key;
    11.     public float Sensitivity, SprintSpeed, SprintMultiplier , SprintSpeedMax , PlayerMain_Speed, DuckMain_Speed, Jump_Power, OriginToFeet_Distance, HeadClearDistance;
    12.     public GameObject Active_Col;
    13.     public bool IsTochingGround, AboveClear;
    14.     public bool Ducking , MeleeCooling;
    15.     public float MeleeCooldown = 10, MeleeDamage, MeleeThrowForce , MeleeDistance, meleeSpeedMultiplier;
    16.     public Slider MeleeSlide;
    17.     public float MaxEquipDist , DistanceFromTarget;
    18.     public GameObject RayObject;
    19.     public GameObject EquipSign;
    20.     public float NonEquippedGunDist;
    21.     public Transform HoldPosition;
    22.     //Non-Public
    23.     RaycastHit AttackedRigidMelee;
    24.     Vector3 Move_Direction;
    25.     RaycastHit GunRayHit;
    26.     void Start()
    27.     {
    28.         Rigid_Player = GetComponent<Rigidbody>();
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         GunSystem();
    35.         Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out AttackedRigidMelee);
    36.         if (AttackedRigidMelee.rigidbody != null)
    37.         {
    38.             DistanceFromTarget = (AttackedRigidMelee.transform.position - transform.position).magnitude;
    39.         }else
    40.         {
    41.             DistanceFromTarget = 0;
    42.         }
    43.  
    44.         if (MeleeCooldown < 0)
    45.         {
    46.             MeleeCooldown = 0;
    47.         }
    48.         MeleeSlide.value = MeleeCooldown / 10;
    49.  
    50.         if (Input.GetMouseButton(1))
    51.         {
    52.             Cursor.visible = false;
    53.         }
    54.         if (MeleeCooldown == 10 && MeleeCooldown > 0)
    55.         {
    56.                 if (MeleeCooldown == 10 && Input.GetKeyDown(Melee_key) && AttackedRigidMelee.transform.gameObject != null && (AttackedRigidMelee.transform.position - transform.position).magnitude < MeleeDistance )
    57.  
    58.             {
    59.                 MeleeCooling = true;
    60.                 AttackedRigidMelee.rigidbody.AddForceAtPosition(PlayerCam.transform.forward * MeleeThrowForce , AttackedRigidMelee.point);
    61.             }
    62.         }
    63.         if (MeleeCooldown == 0 && MeleeCooldown <= 10
    64.             )
    65.         {
    66.             MeleeCooling = false;
    67.          
    68.         }
    69.        if (!Ducking && Input.GetKey(Sprint_Key))
    70.         {
    71.             SprintSpeed = Mathf.Lerp(SprintSpeed, SprintSpeedMax, SprintMultiplier * Time.deltaTime);
    72.         }
    73.        else
    74.         {
    75.             SprintSpeed = 1;
    76.         }
    77.         if (Input.GetKey(Duck_key))
    78.         { Ducking = true; }
    79.         else if (AboveClear)
    80.         { Ducking = false; }
    81.  
    82.         if (Ducking)
    83.         {
    84.             PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position , DuckMode_Transform.position, DuckMain_Speed* Time.deltaTime);
    85.             DuckMode_Col.SetActive(true);
    86.             NormalMode_Col.SetActive(false);
    87.             Active_Col = DuckMode_Col;
    88.         }
    89.         else
    90.         {
    91.             PlayerCam.transform.position = Vector3.Lerp(PlayerCam.transform.position, NormalMode_Transform.position, DuckMain_Speed * Time.deltaTime);
    92.             DuckMode_Col.SetActive(false);
    93.             NormalMode_Col.SetActive(true);
    94.             Active_Col = NormalMode_Col;
    95.         }
    96.  
    97.         //Rigid_Player.transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * Sensitivity);
    98.         RotationY += Input.GetAxisRaw("Mouse X") * Sensitivity;
    99.         RotationX += Input.GetAxisRaw("Mouse Y") * Sensitivity;
    100.         RotationX = Mathf.Clamp(RotationX, MinAngle, MaxAngle);
    101.         Rigid_Player.transform.eulerAngles = new Vector3(0,RotationY,0);
    102.         PlayerCam.transform.eulerAngles = new Vector3(-RotationX, RotationY, 0);
    103.  
    104.  
    105.  
    106.         float HorizontalMovement = Input.GetAxisRaw("Horizontal");
    107.         float VerticalMovement = Input.GetAxisRaw("Vertical");
    108.         Move_Direction = (HorizontalMovement * transform.right + VerticalMovement * transform.forward).normalized;
    109.    
    110.         if (IsTochingGround && Input.GetKeyDown(Jump_key) && AboveClear)
    111.         {
    112.             Rigid_Player.AddForce(Vector3.up * Jump_Power);
    113.         }
    114.     }
    115.     void OnCollisionStay(Collision collision)
    116.     {
    117.         if (Physics.Raycast(transform.position, Vector3.up, HeadClearDistance))
    118.         {
    119.             AboveClear = false;
    120.         }
    121.         else
    122.         {
    123.             AboveClear = true;
    124.         }
    125.  
    126.  
    127.         if (Physics.Raycast(transform.position, Vector3.down, OriginToFeet_Distance))
    128.         {
    129.             IsTochingGround = true;
    130.         }
    131.         else
    132.         {
    133.             IsTochingGround = false;
    134.         }
    135.     }
    136.  
    137.  
    138.     void FixedUpdate()
    139.     {
    140.         if (MeleeCooling)
    141.         {
    142.             MeleeCooldown--;
    143.         }
    144.         else if (MeleeCooldown < 10)
    145.         {
    146.             MeleeCooldown++;
    147.         }
    148.         Walking();
    149.     }
    150.         public void Walking()
    151.     {
    152.         Vector3 FixForY_Axis = new Vector3(0, Rigid_Player.velocity.y, 0);
    153.    
    154.         Rigid_Player.velocity = Move_Direction * PlayerMain_Speed* SprintSpeed * Time.deltaTime;
    155.         Rigid_Player.velocity += FixForY_Axis;
    156.     }
    157.     void GunSystem()
    158.     {
    159.      
    160.         Physics.Raycast(PlayerCam.transform.position, PlayerCam.transform.forward, out GunRayHit);
    161.         if (GunRayHit.rigidbody != null && GunRayHit.transform.GetComponent<GunMain>() != null)
    162.         {
    163.          NonEquippedGunDist = (transform.position - GunRayHit.transform.position).magnitude;
    164.         }
    165.         if(NonEquippedGunDist <= MaxEquipDist  && GunRayHit.rigidbody != null )
    166.         {
    167.             if (GunRayHit.transform.gameObject.GetComponent<GunMain>().IsEquipped == false && GunRayHit.transform.gameObject.tag == "Gun")
    168.             {
    169.                 EquipSign.SetActive(true);
    170.                 if (Input.GetKeyDown(Use_key))
    171.                     GunRayHit.transform.GetComponent<GunMain>().IsEquipped = true;
    172.                 GunRayHit.transform.GetComponent<GunMain>().EquippedPlayer = gameObject;
    173.             }
    174.         }
    175.         else
    176.         {
    177.             EquipSign.SetActive(false);
    178.         }
    179.     }
    180. }
    181.  
    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. :)
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Looks like you forgot to add reference to EquipSign in the inspector window.
     
  3. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    i will lokk into it in a bit thanks for answer
     
  4. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    Nope, İt was Added to reference
     
  5. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    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) ; ?
     
  6. - 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 <variable>; usage instead. So you can set this in the inspector but you cannot change it from other scripts by accident.
     
    Joe-Censored likes this.
  7. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    Yes! you are right. I put "if (GunRayHit.transform != null )" before 167th line and it worked like a charm :)


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


    Thank you guys for your advices. I'm sorry for taking your time.
     
  8. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    Cool, glad that it worked.
     
  9. mero6191

    mero6191

    Joined:
    Dec 29, 2018
    Posts:
    15
    Again, Thank you very much for solution