Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. manilamerc

    manilamerc

    Joined:
    Oct 4, 2012
    Posts:
    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. function OnCollisionEnter(c:Collision)
    17. {
    18.    
    19.    
    20.     if (c.gameObject.name == "Enemy" )
    21.     {
    22.     //currentHealth -= 15.5;
    23.    
    24.     Debug.Log("hit");
    25.     }
    26.    
    27.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    28.     if(healthAmount <=0)
    29.     {
    30.         healthAmount = 0;
    31.         haveHealth = false;
    32.        
    33.     }
    34.  
    35. GUI.UpdateHealthMeter(healthAmount);
    36.  
    37.  
    38.    
    39. }
    40.  


    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

    Joined:
    Dec 2, 2012
    Posts:
    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. function OnCollisionEnter(c:Collision)
    30.  
    31. {
    32.  
    33.    
    34.  
    35.    
    36.  
    37.     if (c.gameObject.name == "Enemy" )
    38.  
    39.     {
    40.  
    41.     //currentHealth -= 15.5;
    42.  
    43.    
    44.  
    45.     Debug.Log("hit");
    46.  
    47.     }
    48.  
    49.    
    50.  
    51.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    52.  
    53.     if(healthAmount <=0)
    54.  
    55.     {
    56.  
    57.         healthAmount = 0;
    58.  
    59.         haveHealth = false;
    60.  
    61.        
    62.  
    63.     }
    64.  
    65.  
    66.  
    67. healthBar2.UpdateHealthMeter(healthAmount);
    68.  
    69.  
    70.  
    71.  
    72.  
    73.    
    74.  
    75. }
    76.  
    If it print's healthbar2 not found then you need to check your tags.
     
  3. manilamerc

    manilamerc

    Joined:
    Oct 4, 2012
    Posts:
    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

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

    hpjohn

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

    manilamerc

    Joined:
    Oct 4, 2012
    Posts:
    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

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    not the tag, the variable name
     
  8. manilamerc

    manilamerc

    Joined:
    Oct 4, 2012
    Posts:
    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

    Joined:
    Oct 4, 2012
    Posts:
    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

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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

    Joined:
    Oct 4, 2012
    Posts:
    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. function OnCollisionEnter(c:Collision)
    32.  
    33. {
    34.  
    35.    
    36.  
    37.    
    38.  
    39.     if (c.gameObject.name == "Enemy" )
    40.  
    41.     {
    42.  
    43.     //currentHealth -= 15.5;
    44.  
    45.    
    46.  
    47.     Debug.Log("hit");
    48.  
    49.     }
    50.  
    51.    
    52.  
    53.     healthAmount -= (healthBurnSpeed*Time.deltaTime);
    54.  
    55.     if(healthAmount <=0)
    56.  
    57.     {
    58.  
    59.         healthAmount = 0;
    60.  
    61.         haveHealth = false;
    62.  
    63.        
    64.  
    65.     }
    66.  
    67.  
    68.  
    69. Bar.UpdateHealthMeter(healthAmount);
    70.  
    71.  
    72.  
    73.  
    74.  
    75.    
    76.  
    77. }
    78.  

    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

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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

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

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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

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

    healthAmount -= 15.5?
     
  16. Dantus

    Dantus

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

    manilamerc

    Joined:
    Oct 4, 2012
    Posts:
    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

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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.