Hi everyone, I am trying to use FindWithTag to search for all of the enemies on the screen with tag “enemy”. The script works fine and it finds all of them, however if all enemies are destroyed, the script sends me an error message saying “NullReference…”, and I have no idea how to fix it :(. I did try using GameObject instead of Transform and it works fine, however later in the script I am trying to use the LookAt which needs “Transform” and gives me error. It doesn’t matter which way I go, it will give me an error, which I cant fix myself.
Do you have any idea how to fix it?
The code:
var target : Transform;
var boltSpeed : float = 10;
function Update()
{
if(!target)
{
var TempSearch = GameObject.FindWithTag("enemy").transform;
if(!TempSearch)
{
Destroy(gameObject, 0.01);
print("dead");
}
else if(TempSearch)
{
target = TempSearch;
}
}
else if(target)
{
transform.LookAt(target);
if(gameObject.transform.position != target.transform.position)
{
gameObject.transform.Translate(Vector3.forward * boltSpeed * Time.deltaTime);
}
}
}
Uploaded with ImageShack.us
I got now a new error <a target='_blank' href='http://imageshack.us/photo/my-images/5/unledmp.jpg/'><img src='http://img5.imageshack.us/img5/4236/unledmp.th.jpg' border='0'/></a> Uploaded with <a target='_blank' href='http://imageshack.us'>ImageShack.us</a>
– warkarmaAssumed that you were using transform in your script since you had .transform at the end. Just take out the : Transform. Edited my answer.
– FTheCloudSame error
– warkarmaOh I see The Problem. Its when you say target = tempSearch. You cant do that because you're defining the var target as Transform and the var tempSearch as a GameObject. They can't equal eachother because there not the same type. Also at the lookat function, thats when you use the .transform. Changed my answer again, again to show you what it should look like.
– FTheCloudYey, it works fine now :) Thanks a lot
– warkarma