- Home /
NullReferenceException help! (Linking two javascipt files)
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!
Answer by LVBen · Jun 04, 2013 at 02:42 AM
Which var has the null reference?
It points to the "eatFood.isEating = true;" when I click on the red console message.
NullReferenceException: Object reference not set to an instance of an object
Item.DoEatFood () (at Assets/Inventory/Item.js:146)
Item.Update () (at Assets/Inventory/Item.js:83)
It looks like you are destroying the object and then trying to use a var (eatFood) inside of the object. Try changing the order of these two lines of code:
Destroy (gameObject);
eatFood.isEating = true;
That sounded like a good suggestion, I tried it and unfortunately it still gave me:
NullReferenceException: Object reference not set to an instance of an object
Item.DoEatFood () (at Assets/Inventory/Item.js:154)
Item.Update () (at Assets/Inventory/Item.js:88)
It's possibly worth mentioning that it won't let me drag my Health GameObject into this field on the Item.js script:
Oh and just incase you are wondering, when it's commented out like this, it works fine and I see the debug when I eat it.
Item.js debug: function DoEatFood(){
if(isFood)
{
//eatFood.isEating = true;
Destroy (gameObject);
Debug.Log("Item Eaten");
}
}
Where is the code that is calling DoEatFood? One suggestion is to try and narrow down the code that is causing the problem. Try to eli$$anonymous$$ate as much code as you possibly can and get the smallest amount of code possible that still results in the issue. Sometimes if you do that, the problem will be incredibly obvious. Even if it is not totally obvious at that point, then it will make it much simpler to deter$$anonymous$$e the issue.
Answer by Exagerate · Jun 04, 2013 at 08:33 PM
I've figured out how to do it!
Script1 added a variable:
var eatFood : HealthBar;
In the first script I needed to call the function in the Start:
function Start() {
Debug.Log("Healthbar Loaded!");
eatFood = new HealthBar();
}
And then:
function DoEatFood(){
if(isFood)
{
Destroy (gameObject);
GameObject.Find("Health").GetComponent(HealthBar).Eating();
}
}
In the second script I didn't need any variable, just the function:
function Eating()
{
health +=1;
Debug.Log("Player ate food!");
}
Thanks for your help!
Your answer
