NullReferenceException: Object reference not set to an instance of an object (500477)

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

#pragma strict

var healthBurnSpeed: float;
var haveHealth:boolean;
var healthAmount: float;
var GUI:HealthBar2;


function Start () 
{
	GUI = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);
}



function OnCollisionEnter(c:Collision)
{
	
	
	if (c.gameObject.name == "Enemy" )
	{
	//currentHealth -= 15.5;
	
	Debug.Log("hit");
	}
	
	healthAmount -= (healthBurnSpeed*Time.deltaTime);
	if(healthAmount <=0)
	{
		healthAmount = 0;
		haveHealth = false;
		
	}

GUI.UpdateHealthMeter(healthAmount);


	
}

Here is my other Code

var healthMeter: GUITexture;
var healthMeterStartingWidth:float;

function Start () 
{
healthMeterStartingWidth = healthMeter.pixelInset.width;
}





function UpdateHealthMeter(newHealthAmount:float)
{
	healthMeter.pixelInset.width = healthMeterStartingWidth * (newHealthAmount* .01);
}

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.

var healthBurnSpeed: float;

var haveHealth:boolean;

var healthAmount: float;

var healthBar2:HealthBar2;

 

 

function Start () 

{

    healthBar2 = GameObject.FindWithTag("GUI").GetComponent(HealthBar2);
    if (healthBar2 == null) Debug.Log("Could not find HealthBar2");

}

 

 

 

function OnCollisionEnter(c:Collision)

{

    

    

    if (c.gameObject.name == "Enemy" )

    {

    //currentHealth -= 15.5;

    

    Debug.Log("hit");

    }

    

    healthAmount -= (healthBurnSpeed*Time.deltaTime);

    if(healthAmount <=0)

    {

        healthAmount = 0;

        haveHealth = false;

        

    }

 

healthBar2.UpdateHealthMeter(healthAmount);

 

 

    

}

If it print’s healthbar2 not found then you need to check your tags.

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.

Yeah, GUI is probably a specific named thing, and you shouldnt be trying to assign things to it.

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.

not the tag, the variable name

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?

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.

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

var healthBurnSpeed: float;

var haveHealth:boolean;

var healthAmount: float;

var Bar:HealthBar2;

 

 

function Start () 

{

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

}

 

 

 

function OnCollisionEnter(c:Collision)

{

    

    

    if (c.gameObject.name == "Enemy" )

    {

    //currentHealth -= 15.5;

    

    Debug.Log("hit");

    }

    

    healthAmount -= (healthBurnSpeed*Time.deltaTime);

    if(healthAmount <=0)

    {

        healthAmount = 0;

        haveHealth = false;

        

    }

 

Bar.UpdateHealthMeter(healthAmount);

 

 

    

}

Here is my other code

var healthMeter: GUITexture;

var healthMeterStartingWidth:float;

 

function Start () 

{

healthMeterStartingWidth = healthMeter.pixelInset.width;
Debug.Log ("working");

}

 

 

 

 

 

function UpdateHealthMeter(newHealthAmount:float)

{

    healthMeter.pixelInset.width = healthMeterStartingWidth * (newHealthAmount* .01);
Debug.Log ("working2");
}

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:

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?

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.

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.

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:

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.