Search Unity

[SOLVED] Errors in console, but game works good

Discussion in 'Scripting' started by sonys222, Nov 25, 2015.

  1. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    Hello. What is wrong with my code? Error:
    NullReferenceException: Object reference not set to an instance of an object
    Health.Update () (at Assets/Health.cs:23)



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour {
    7.  
    8.     public Slider health;
    9.     public int obrazenia=10;
    10.     public int hp=100;
    11.  
    12.  
    13.  
    14.     public Text hptext;
    15.  
    16.     void Start ()
    17.     {
    18.         health.value = hp;
    19.     }
    20.    
    21.     public void Update()
    22.     {
    23.         health.value = hp;
    24.         PokazywanieHp ();
    25.     }
    26.  
    27. public void Odejmowanie()
    28.     {
    29.         hp = hp - obrazenia;
    30.     }
    31.  
    32.  
    33.  
    34.     void PokazywanieHp()
    35.     {
    36.         hptext.text = "HEALTH: " + health.value;
    37.     }
    38.  
    39. }
    40.  
    41.  
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    6,267
    Is "health" pointing to a Slider properly?
     
  3. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
  4. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    If it is properly pointing to a Slider component, you can add a small check in your update method like this:
    Code (CSharp):
    1. public void Update()
    2. {
    3.     if (health == null)
    4.     {
    5.         Debug.LogError("Slider is null:"+health);
    6.         return;
    7.     }
    8.  
    9.     health.value = hp;
    10.     PokazywanieHp ();
    11. }
    If your output log starts throwing this error, it happens that your slider gets destroyed somehow. I would advise to check other scripts.

    Also, I have to nitpick about this:
    Code (CSharp):
    1. public class Health : MonoBehaviour
    2. {
    3.     public Slider health;     //<--------- naming!
    4.  
    5.     //rest of code...
    6. }
    You have a class named Health and another variable named health as well. You could say it doesn't really matter for your small project, but in the long run it is worth to keep variable naming proper. Since it is a slider, you could name it:
    Code (CSharp):
    1. public class Health : MonoBehaviour
    2. {
    3.     public Slider slider;
    4.     public Slider sliderObject;
    5.     public Slider valueSlider;
    6.  
    7.     //rest of code...
    8. }
    Also, since Health is your component, it is logical that it contains health. Another nitpick, although not really a problem when working alone, is scripting in native language. In case someone joined your team and knows only english, he will waste a lot of time and nerves trying to google translate every function name. >_>

    Best of luck!
     
    sonys222 likes this.
  5. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    I had two of the same scripts in one GameObject haha. The problem is solved. Thanks.
     
unityunity