- Home /
Question by
lijrobert · Jan 15, 2012 at 03:00 AM ·
javascriptnullreferenceexceptionhealth bar
Help with NullReferenceException
var H00 : Texture2D;
var H10 : Texture2D;
var H20 : Texture2D;
var H30 : Texture2D;
var H40 : Texture2D;
var H50 : Texture2D;
var H60 : Texture2D;
var H70 : Texture2D;
var MAX_HEALTH = 70;
var HEALTH = 70;
function Update()
{
var g_Health = GameObject.Find("g.Health");
if(HEALTH > MAX_HEALTH){
HEALTH = MAX_HEALTH;
}
if(HEALTH > 60){
g_Health.guiTexture.texture = H70;
}
else if(HEALTH > 50){
g_Health.guiTexture.texture = H60;
}
else if(HEALTH > 40){
g_Health.guiTexture.texture = H50;
}
else if(HEALTH > 30){
g_Health.guiTexture.texture = H40;
}
else if(HEALTH > 20){
g_Health.guiTexture.texture = H30;
}
else if(HEALTH > 10){
g_Health.guiTexture.texture = H20;
}
else if(HEALTH > 0){
g_Health.guiTexture.texture = H10;
}
else if(HEALTH <= 0){
g_Health.guiTexture.texture = H00;
return;
}
}
from this javascript I get this error NullReferenceException Health.Update () (at Assets/Scripts/Health.js:19) and I have no idea why PLEASE HELP
Comment
Answer by Bunny83 · Jan 15, 2012 at 03:03 AM
This can be caused be several things:
Maybe your Gameobject with the guitexture doesn't have the name "g.Health".
Maybe there are more than one objects with that name.
The GameObject with the name "g.Health" doesn't have a GUITexture component attached.
I continue to be amazed by my own stupidey, it was named Health not g.Health Slap Face