- Home /
Why is this code getting NullReferenceException
I have this code that refers to another script called PlayerStatus.js which includes a simple function addPuz() but in game, it tells me there is this error. I've been looking for ages and trying things such as using GameObject or GetComponent and as but nothing seems to work. I'm not very experienced but this seems pretty simple of calling a function in another script so I'm not sure why it won't work.
var Object1 : Transform;
var Object2 : Transform;
var dist = 1;
function Update ()
{
}
if(Vector3.Distance(Object1.transform.position, Object2.transform.position) < dist)
{
var playerStatus : PlayerStatus;
playerStatus.addPuz();
Destroy(this);
}
And the playerStatus is just a reference to a PlayerStatus type but there is nothing in there. So you need to find the object you want to deal with or you instantiate one.
Also, Destroy(this); will destroy the script, not the object.
Answer by BilboStabbins · Mar 11, 2013 at 04:50 PM
Hi SithArrow,
Assuming that the playerStatus script is attached to the Player, you would first need a reference to the Player object to be able to reference the script. What you've done by saying, var playerStatus PlayerStatus is created an instance of A PlayerStatus script, that's all, not one attached to anything. To reference a specific script instance you have to reference the object its attached to first, then use GetComponent to access the script attached to it.
So, something like:
function Update()
{
// Reference the PlayerStatus script attached to Player (gameObject) and
// assign it to playerStatus instance
playerStatus = gameObject.GetComponent(PlayerStatus);
// Note, GetComponent(PlayerStatus) will also work as 'gameObject.' is assumed
// if nothing is placed before GetComponent
// Use the instance to call the addPuz() function
playerStatus.addPuz();
}
Edit - Yes, Fafase and Nelis are correct (wasn't looking at that, d'oh!). It should be in Update(), and the Destroy function will destroy the script. To destroy the object the script is attached to, use:
Destroy(gameObject);
I hope that helps.
Answer by SithArrow · Mar 11, 2013 at 08:44 PM
Took me a bit to figure it out but where you put gameObject I realized i needed to make a var object and then drag over the player in Unity or atleast I worked out that way. So thank you very much. I do realize Destroy(this) only destroys the script and that's what I want. Also I'm not exactly sure why it should be in the update bit because either way it should only run through once. Or I'm just missing the point of the update function. All in all it works now so thank you very much for tending to my ignorance.
It does run once. It runs when the distance between the two objects are less than your distance variable.
It should go in the update function because it has to CHEC$$anonymous$$ if the distance between the two objects are less than variable distance, EVERY frame. So, that's why it should be in Update().
Your answer
Follow this Question
Related Questions
NullReferenceException Error 6 Answers
Instantiate() as GameObject = null reference 1 Answer
Null Reference Exception error 3 Answers
Null Reference exception error 2 Answers