Unity Community


Results 1 to 18 of 18

  1. Posts
    102

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

    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:  
    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. }



    Here is my other Code

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


  2. 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(HealthB ar2);

    try this code.

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

    If it print's healthbar2 not found then you need to check your tags.
    Despite the high costs of living, it still remains popular.

    Remember to say thanks, it helps boost morale.

    Check my personal blog: www.mrmoss.net


  3. 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 by manilamerc; 03-22-2013 at 06:04 PM.


  4. Posts
    513
    Quote Originally Posted by manilamerc View Post
    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
    I hope you renamed GUI to something else.
    Despite the high costs of living, it still remains popular.

    Remember to say thanks, it helps boost morale.

    Check my personal blog: www.mrmoss.net



  5. 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.



  6. 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?


  7. 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


  8. Posts
    2,878
    Quote Originally Posted by manilamerc View Post
    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
    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.


  9. 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:  
    1.  
    2.  
    3. var healthBurnSpeed: float;
    4.  
    5. var haveHealth:boolean;
    6.  
    7. var healthAmount: float;
    8.  
    9. var Bar:HealthBar2;
    10.  
    11.  
    12.  
    13.  
    14.  
    15. function Start ()
    16.  
    17. {
    18.  
    19.     Bar = GameObject.FindWithTag("GUI").GetComponent(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. Bar.UpdateHealthMeter(healthAmount);
    67.  
    68.  
    69.  
    70.  
    71.  
    72.    
    73.  
    74. }


    Here is my other code

    Code:  
    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. }


  10. Posts
    2,878
    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:  
    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?


  11. Posts
    102
    what that line is supposed to do is remove health per second at the rate of the burnspeed


  12. Posts
    2,878
    Quote Originally Posted by manilamerc View Post
    what that line is supposed to do is remove health per second at the rate of the burnspeed
    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.


  13. Posts
    102
    Hmmm I see.. How should I this out then? Should I do something like

    healthAmount -= 15.5?


  14. Posts
    2,878
    Quote Originally Posted by manilamerc View Post
    Hmmm I see.. How should I this out then? Should I do something like

    healthAmount -= 15.5?
    I don't know if that is what you want to achieve. But for me it sounds reasonable.


  15. 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?


  16. Posts
    2,878
    Quote Originally Posted by manilamerc View Post
    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?
    If you are not getting error messages, then it should already work. If you don't see it, then check this line:
    Code:  
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •