I have two separate scripts, one of them is attached to an edible object, and the other is my health bar. I am having trouble linking the two together (A null reference, probably something to do with an incorrect var?). It doesn’t throw the null until the object is eaten, which makes me think i’ve linked the files together wrongly, or it shouldn’t be a boolean (I have no idea what it SHOULD be, if that’s the case). So I wonder if someone with better Javascript experience might be able to point me in the right direction. It’s all been guesswork so far!
Edible item script (Item.js)
var eatFood : HealthBar;
var isFood : boolean = false;
...
//Player will be able to eat the item (destroy it and add health) if the item is flagged as Food.
function DoEatFood(){
if(isFood)
{
Destroy (gameObject);
eatFood.isEating = true;
}
}
Health bar script (HealthBar.js)
#pragma strict
var barDisplay : float = 0;
var health = 1;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
public var isEating : boolean = false;
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update()
{
// for this example, the bar display is linked to the current time,
// however you would set this value based on your desired display
// eg, the loading progress, the player's health, or whatever.
barDisplay = health - (Time.time / 1000);
if (barDisplay<=0) {
Death();
}
}
function Death()
{
Application.LoadLevel (0);
Debug.Log("Player Died");
}
function Eating()
{
if(isEating)
{
barDisplay +=0.3;
Debug.Log("Player ate food!");
}
}
Thanks community!