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

Discussion in 'Scripting' started by manilamerc, Mar 23, 2013.

  1. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    So I want my code to lower down the healthbar if my player gets hit by a gameobject called Enemy
    and I get this error

    NullReferenceException: Object reference not set to an instance of an object
    Collision2.OnCollisionEnter (UnityEngine.Collision c) (at Assets/Scripts/Collision2.js:35)

    I would like to know what this error means exactly

    Here's my code

    Code (csharp):
    1. #pragma strict
    2.  
    3. var healthBurnSpeed: float;
    4. var haveHealth:boolean;
    5. var healthAmount: float;
    6. var GUI:HealthBar2;
    7.  
    8.  
    9. function Start ()
    10. {
    11.     GUI = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);
    12. }
    13.  
    14.  
    15.  
    16. {
    17.    
    18.    
    19.     if (c.gameObject.name == "Enemy" )
    20.     {
    21.     //currentHealth -= 15.5;
    22.    
    23.     Debug.Log("hit");
    24.     }
    25.    
    26.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    27.     if(healthAmount <=0)
    28.     {
    29.         healthAmount = 0;
    30.         haveHealth = false;
    31.        
    32.     }
    33.  
    34. GUI.UpdateHealthMeter(healthAmount);
    35.  
    36.  
    37.    
    38. }
    39.  


    Here is my other Code

    Code (csharp):
    1.  
    2.  
    3. var healthMeter: GUITexture;
    4. var healthMeterStartingWidth:float;
    5.  
    6. function Start ()
    7. {
    8. healthMeterStartingWidth = healthMeter.pixelInset.width;
    9. }
    10.  
    11.  
    12.  
    13.  
    14.  
    15. function UpdateHealthMeter(newHealthAmount:float)
    16. {
    17.     healthMeter.pixelInset.width = healthMeterStartingWidth * (newHealthAmount* .01);
    18. }
  2. EliteMossy

    EliteMossy

    New Member

    Joined:
    Dec 2, 2012
    Messages:
    513
    You should rename GUI to something else, bad practice to use GUI when it is already something.

    The problem is probably to do with the line in Start()

    GUI = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);

    try this code.

    Code (csharp):
    1.  
    2. var healthBurnSpeed: float;
    3.  
    4. var haveHealth:boolean;
    5.  
    6. var healthAmount: float;
    7.  
    8. var healthBar2:HealthBar2;
    9.  
    10.  
    11.  
    12.  
    13.  
    14. function Start ()
    15.  
    16. {
    17.  
    18.     healthBar2 = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);
    19.     if (healthBar2 == null) Debug.Log("Could not find HealthBar2");
    20.  
    21. }
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30. {
    31.  
    32.    
    33.  
    34.    
    35.  
    36.     if (c.gameObject.name == "Enemy" )
    37.  
    38.     {
    39.  
    40.     //currentHealth -= 15.5;
    41.  
    42.    
    43.  
    44.     Debug.Log("hit");
    45.  
    46.     }
    47.  
    48.    
    49.  
    50.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    51.  
    52.     if(healthAmount <=0)
    53.  
    54.     {
    55.  
    56.         healthAmount = 0;
    57.  
    58.         haveHealth = false;
    59.  
    60.        
    61.  
    62.     }
    63.  
    64.  
    65.  
    66. healthBar2.UpdateHealthMeter(healthAmount);
    67.  
    68.  
    69.  
    70.  
    71.  
    72.    
    73.  
    74. }
    75.  
    If it print's healthbar2 not found then you need to check your tags.
  3. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    ok this is really weird.. the health bar goes down only when I restart my game. But it doesn't go down in game. Someone please help me

    and EliteMossy I did not use that code you put up, I just tried fixing it. So I'm using the same code I originally posted
    Last edited: Mar 23, 2013
  4. EliteMossy

    EliteMossy

    New Member

    Joined:
    Dec 2, 2012
    Messages:
    513
    I hope you renamed GUI to something else.
  5. hpjohn

    hpjohn

    Member

    Joined:
    Aug 14, 2012
    Messages:
    991
    Yeah, GUI is probably a specific named thing, and you shouldnt be trying to assign things to it.
  6. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    I changed the tag GUI, but I think it's not working because I'm using OnCollisionEnter function instead of function Update(). But I could be wrong... I dunno why the health bar doesn't go down in game.
  7. hpjohn

    hpjohn

    Member

    Joined:
    Aug 14, 2012
    Messages:
    991
    not the tag, the variable name
  8. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    I changed the variable name to "Bar2" and the health doesn't go down in game. Could this have something to do with my OnEnterCollison?
  9. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    Ok here's my question. Is there a way to use an OnEnterCollision function within an Update function? Because the GUI will not update on screen without an Update function. When I try using an OnEnterCollision within an Update function it creates errors. All I want is if player collides with enemy then update gui health bar
  10. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,737
    You have to place the code in OnEnterCollision. Could you post your current code and the exact error message you are getting. It is important that you always check the very first error message if there is more than one.
    It seems that the health bar is not found in your Start function. EliteMossy posted code to illustrate how you can find it out.
  11. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    I have no errors, only problem is my health doesn't go down as I'm playing the game. It only goes down when I restart the game.
    Here is
    Code (csharp):
    1.  
    2.  
    3.  
    4.  
    5. var healthBurnSpeed: float;
    6.  
    7. var haveHealth:boolean;
    8.  
    9. var healthAmount: float;
    10.  
    11. var Bar:HealthBar2;
    12.  
    13.  
    14.  
    15.  
    16.  
    17. function Start ()
    18.  
    19. {
    20.  
    21.     Bar = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);
    22.  
    23. }
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32. {
    33.  
    34.    
    35.  
    36.    
    37.  
    38.     if (c.gameObject.name == "Enemy" )
    39.  
    40.     {
    41.  
    42.     //currentHealth -= 15.5;
    43.  
    44.    
    45.  
    46.     Debug.Log("hit");
    47.  
    48.     }
    49.  
    50.    
    51.  
    52.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    53.  
    54.     if(healthAmount <=0)
    55.  
    56.     {
    57.  
    58.         healthAmount = 0;
    59.  
    60.         haveHealth = false;
    61.  
    62.        
    63.  
    64.     }
    65.  
    66.  
    67.  
    68. Bar.UpdateHealthMeter(healthAmount);
    69.  
    70.  
    71.  
    72.  
    73.  
    74.    
    75.  
    76. }
    77.  

    Here is my other code

    Code (csharp):
    1. var healthMeter: GUITexture;
    2.  
    3. var healthMeterStartingWidth:float;
    4.  
    5.  
    6.  
    7. function Start ()
    8.  
    9. {
    10.  
    11. healthMeterStartingWidth = healthMeter.pixelInset.width;
    12. Debug.Log ("working");
    13.  
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26. function UpdateHealthMeter(newHealthAmount:float)
    27.  
    28. {
    29.  
    30.     healthMeter.pixelInset.width = healthMeterStartingWidth * (newHealthAmount* .01);
    31. Debug.Log ("working2");
    32. }
    33.  
  12. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,737
    If people try to help you, it would be nice to make the code at least slightly readable...

    What exactly are you trying to achieve? This line makes no sense for me:
    Code (csharp):
    1. healthAmount -= (healthBurnSpeed*Time.deltaTime);
    When the collision starts, a certain amount of health needs to be subtracted. Why do you multiply it with Time.deltaTime?
  13. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    what that line is supposed to do is remove health per second at the rate of the burnspeed
  14. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,737
    Of course, but this is only called when it enters a collision. It doesn't decrease the health while the collision takes place, but only when it enters the collision.
  15. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    Hmmm I see.. How should I this out then? Should I do something like

    healthAmount -= 15.5?
  16. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,737
    I don't know if that is what you want to achieve. But for me it sounds reasonable.
  17. manilamerc

    manilamerc

    New Member

    Joined:
    Oct 4, 2012
    Messages:
    102
    well sure having the health drop down by 15.5 is fine for me but how do I make sure the gui texture shrinks down in game?
  18. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,737
    If you are not getting error messages, then it should already work. If you don't see it, then check this line:
    Code (csharp):
    1. healthMeter.pixelInset.width = healthMeterStartingWidth * (newHealthAmount* .01);
    Maybe there is something wrong. Check if the values are really passed to this passage and if so does this line what you expected from it.