Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Wswingler, Aug 18, 2023.

  1. Wswingler

    Wswingler

    Joined:
    Jul 3, 2023
    Posts:
    2
    Hi, I'm new to unity and trying to create a health system in a game I am making. But I keep getting this error:
    NullReferenceException: Object reference not set to an instance of an object

    This is the script for changing the Health Bar and it says the error message is on the line, Healthslider.value = health:
    Code (CSharp):
    1. public class HealthBarScript : MonoBehaviour
    2. {
    3.     public Slider HealthSlider;
    4.     public UnitHealth _playerHealth = new UnitHealth(100, 100);
    5.  
    6.  
    7.     private void start()
    8.     {
    9.         HealthSlider = GetComponent<Slider>();
    10.     }
    11.  
    12.     public void SetHealth(int Health)
    13.     {
    14.         HealthSlider.value = Health;
    15.     }
    16.     public void SetMaxHealth(int maxHealth)
    17.     {
    18.         HealthSlider.maxValue = maxHealth;
    19.         HealthSlider.value = maxHealth;
    20.     }
    21.  
    22. }


    This is the script I am calling the function from:
    Code (CSharp):
    1. public class playerBehavouir : MonoBehaviour
    2. {
    3.     [SerializeField] HealthBarScript healthbar;
    4.     public UnitHealth _playerHealth = new UnitHealth(100, 100);
    5.     public int test;
    6.  
    7.     void Start()
    8.     {
    9.         healthbar.SetMaxHealth(GameManager.gameManager._playerHealth.MaxHealth);
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if(Input.GetKeyDown(KeyCode.G))
    15.         {
    16.             PlayerTakeDamage(20);
    17.             Debug.Log(GameManager.gameManager._playerHealth.Health);
    18.         }
    19.         if (Input.GetKeyDown(KeyCode.F))
    20.         {
    21.             PlayerHeal(10);
    22.             Debug.Log(GameManager.gameManager._playerHealth.Health);
    23.         }
    24.     }
    25.  
    26.     private void PlayerTakeDamage(int Damage)
    27.     {
    28.         GameManager.gameManager._playerHealth.DamageUnit(Damage);
    29.         healthbar.SetHealth(GameManager.gameManager._playerHealth.Health);
    30.     }
    31.     private void PlayerHeal(int Healing)
    32.     {
    33.         GameManager.gameManager._playerHealth.HealUnit(Healing);
    34.         healthbar.SetHealth(GameManager.gameManager._playerHealth.Health);
    35.     }
    36. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    simple typo line 7. Code is very picky about case-sensitive stuff. So the compiler see's your void of
    start()
    as your own created method, rather than Unity's built in method of
    Start()
     
  3. Wswingler

    Wswingler

    Joined:
    Jul 3, 2023
    Posts:
    2
    Thank you for pointing that out, I still get the same error though
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Null reference is a C# thing, not a Unity thing. You're using a reference that isn't set to anything i.e. it's NULL. If you don't know what this is, you need to read up on what C# value types versus references types are.

    This is the most common thing reported by people who are new to C#. We have a sticky thread because of this here: https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    If "HealthSlider" isn't assigned, it'll be NULL. You tried to set this in "start" but you didn't use the correct case, hence the comment above. Saying it still doesn't work isn't useful. This will assign it to the "HealthSlider" but if there isn't a component of that type, it'll assign it to NULL.

    You need to learn how to debug your code so please follow this link here: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  6. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'd say another typo issue, line 3, 9, 14, 18, 19. As in line 3 that should be a lower case name, so you never get confused on what is a full class, versus some variable you declared at the top of the script. But to be honest, it should still work.

    So since it's a public reference, can you not just put it in the proper slot, in the Inspector? And do away with trying to get it's component, since it already would have it?

    But if you want to get it's component through script, you need to define it's path, and on which object it's on. Unless "Slider" is a component itself, on said object with the same script your using(which I don't think it is).
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You should look at the following diagram too: https://docs.unity3d.com/Manual/ExecutionOrder.html

    You cannot guarantee that "Start" is called on "HealthBarScript" before "playerBehavouir". You can change the execution order but the best way is to do any initialization such as getting components (etc) in "Awake" then, if you're touching other components that need to be initialized already as you do in "playerBehavouir", you should do that in "Start" which is called after all the components have had "Awake" called.

    Do this:
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         HealthSlider = GetComponent<Slider>();
    4.     }