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();
}
[4420-console+error.png|4420]
easy 
the GameObject.FindWithTag (“target01Tag”); return null
or the GameObject.FindWithTag (“target01Tag”); return null
the one on line 25
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?
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.