Search Unity

NullReferenceException: Object reference not set to an instance of an object;

Discussion in '2D' started by Nitj-Sefni, Jan 14, 2020.

  1. Nitj-Sefni

    Nitj-Sefni

    Joined:
    Jan 14, 2020
    Posts:
    4
    I want to make health bar. That will move after my hero in the right corner of camera.
    But i got this error in unity console if someone can help I will be really greatful: NullReferenceException: Object reference not set to an instance of an object;
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class HealthBar : MonoBehaviour {
    6.     float x;
    7.     float y;
    8.     float dx;
    9.     float dy;
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         dx = transform.position.x - Camera.main.gameObject.transform.position.x;
    14.         dy = transform.position.y - Camera.main.gameObject.transform.position.y;
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update (){
    19.         Pozycja();
    20.     }
    21.     void Pozycja()
    22.     {
    23.         x = Camera.main.gameObject.transform.position.x + dx;
    24.         y = Camera.main.gameObject.transform.position.y + dy;
    25.         transform.position = new Vector3(x, y, transform.position.z);
    26.     }
    27. }
    28.  
    29.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,181
    The error is as it says, you have a reference to an object that is NULL. It'll also tell you the actual line number. There are no object references in this code above which makes me wonder why you've posted it. Does the error indicate it's in this code?
     
  3. Nitj-Sefni

    Nitj-Sefni

    Joined:
    Jan 14, 2020
    Posts:
    4
    Yes this error indicate in line 13 and 23 of my script and I don't know why.
     
    Last edited: Jan 14, 2020
  4. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,210
    There s no line 30 in the code above. Are you sure the error is with this script?
     
  5. Nitj-Sefni

    Nitj-Sefni

    Joined:
    Jan 14, 2020
    Posts:
    4
    23 and 13 my mistake
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,181
    I would recommend looking up what the error actually means as I described above. Also, you can easily debug it by setting breakpoints on those lines and inspecting what is actually NULL. That's far easier than waiting for a response on the forums and will get you moving forward quicker.

    Anyway, I presume you've checked to see that you actually have a main camera?

    https://docs.unity3d.com/ScriptReference/Camera-main.html
     
  7. Nitj-Sefni

    Nitj-Sefni

    Joined:
    Jan 14, 2020
    Posts:
    4
    Ok thank you very much I will look it up thanks a lot.
     
    MelvMay likes this.
unityunity