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:
    11,500
    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,561
    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:
    11,500
    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.
  8. matteoP2k5

    matteoP2k5

    Joined:
    Apr 28, 2017
    Posts:
    1
    Melv May, I have the same problem but the other script isn't null, There is the public variable that i want but it gave me this error, how can i fix ?
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This is a user error where you're referring to an object you've not set in the script. I cannot really help you fix it. Just look at the line and see why it's not set.
     
  10. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Okay I think the problem has to do with the Camera.main that you are calling, the reason is most likely that you removed the camera that comes with the scene once you start a new project and maybe added a new camera.

    The reason that it is giving you null is because your current camera has not been tagged as MainCamera, select the camera object and make sure it is tagged with the MainCamera tag, because unless you have a camera with that tag you can not use Camera.main, since their is no main camera.

    https://docs.unity3d.com/540/Documentation/Manual/Tags.html