- Home /
Unknown Error.
I have gotten an error on my project and can't figure out what the problem is, if anyone can help, i'll be thankful. Pic of error and script with problem.
function Start () {
Destroy(this.gameObject, 3);
}
function Update () {
}
function OnCollisionEnter(col:Collision){
Debug.Log("collided");
//Here we are searching for an object with the tag 'target01Tag'.
var object = GameObject.FindWithTag ("target01Tag");
// Here we are looking for the script called 'DestroyTarget' to use.
var objectScript = object.GetComponent (DestroyTarget);
//'Here we are calling the function in the script we want to use.
objectScript.DestroyMe();
}
Answer by Paul-Sinnett · Oct 25, 2012 at 01:13 PM
You're getting null returned from FindWithTag. So when you try to use "object" it gives you a NullReferenceException. This means "objectScript" will also be null, so you get another NullReferenceException when you try to use that.
Have you added the tag "target01Tag" to at least one object in your scene?
Looking at your script, it looks like some kind of projectile; where the idea is that the projectile will destroy a target if it hits it. If so:
function OnCollisionEnter(col:Collision){
Debug.Log("collided");
//Here we are searching for an object with the tag 'target01Tag'.
var object = col.gameObject;
if (object && object.CompareTag("target01Tag")) {
Debug.Log("target01Tag object hit");
// Here we are looking for the script called 'DestroyTarget' to use.
var objectScript = object.GetComponent (DestroyTarget);
if (objectScript) {
//'Here we are calling the function in the script we want to use.
objectScript.Destroy$$anonymous$$e();
} else {
Debug.Log("hit target01Tag object but DestroyTarget script not found");
}
} else {
Debug.Log("hit something else");
}
}
All the "Debug.log" are co$$anonymous$$g up, but the error still remains when the bullet hits the target.
I've added another check to the code above to ensure that there's a DestroyTarget script attached.
Answer by cfloutier · Oct 25, 2012 at 01:09 PM
easy :) the GameObject.FindWithTag ("target01Tag"); return null
or the GameObject.FindWithTag ("target01Tag"); return null
the one on line 25
Answer by Graham-Dunnett · Oct 25, 2012 at 01:11 PM
Hey there,
You do not have an unknown error. You have a Null Reference Exception. The console even tells you that it's on line 25 of your Destroy.js script - that's what the :25 means on the end of the line. You don't share with us what line 25 is, however, I suspect that it's the line:
var objectScript = object.GetComponent (DestroyTarget);
but I am totally guessing. A Null Reference Exception happens when you go to use an object but it's not been set. So, I suspect the line before has failed to find a game object with the tag target01Tag, and so your variable called object is not set to anything. So, use and if() statement to check for GameObject.FindWithTag returning null.
Sorry, I am kinda new to Unity, What would the code look like for the if() statement.
Your answer
Follow this Question
Related Questions
Help with changing spririterenderer's sprite 2 Answers
Do runtime errors slow down your game? 2 Answers
Main Menu Script Issues 1 Answer