nullreference issue... stumped

Hello all, thanks for all your help so far and in advance on this one and I’m almost done with this script (I think) and just having one more issue and I am not sure why…
Getting the error:

NullReferenceException: Object reference not set to an instance of an object
turretai2-1.fire () (at Assets/Scripts/turretai2-1.js:33)

Which is the vector3 line to shoot at the target. I have the object sourced, it stays sourced while playing. I have no idea why I’m still getting error and just really need a hand finishing this up… Thanks again all,

Gaspar

private var ammoprefab: Transform;
private var nextTime : float;
	//start shooting
function Start () {
    InvokeRepeating ("fire", 0, 0.3);
}
    // Finding the closest target
function find() : GameObject {
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance; 
            } 
    } 
    return closest;    
}
	//used to look at the target
function Update(){
var target = find();
transform.LookAt(target.transform);
}
	//used to determine target and distance to shoot at
function fire(){
var target = find();
var range = 500;
if(Vector3.Distance(transform.gameObject.position, target.gameObject.position) < range)
shoot();
}
	//used to fire the bullet
function shoot(){
ammoprefab = transform.FindObject("newbullet");
	var ammo = 
		Instantiate(ammoprefab,transform.Find("f2").transform.position, transform.rotation);
		ammo.rigidbody.AddForce(transform.forward *10000);
		Destroy(ammo.gameObject, 1.5); 
	}

target isn’t set to anything. Surround transform.LookAt(target.transform); with an if statement, so it looks like the following:

if(target)
{
    transform.LookAt(target.transform);
}

Then take var target out as a field, since you’re reusing it so many times (and you will only need one target at a time). This isn’t necessary to fix the issue you’re having here, but it is far more practical, and will make the game a -lot- more stable and faster running, if you get into the habit of managing scope a bit better.

Once you’ve made var target a field, be sure to get rid of all the declarations of var target in the functions, then check, with an if statement, if it exists (the same way as above), before calling target for anything. Your resulting script should look like this:

private var ammoprefab: Transform;
private var nextTime : float;
private var target : GameObject;
    //start shooting
function Start () {
    InvokeRepeating ("fire", 0, 0.3);
}
    // Finding the closest target
function find() : GameObject {
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance; 
            } 
    } 
    return closest;    
}
    //used to look at the target
function Update()
{
    target = find();
    transform.LookAt(target.transform);
}
    //used to determine target and distance to shoot at
function fire(){
var range = 500;
if(target)
{
    if(Vector3.Distance(transform.position, target.transform.position) < range)
    {
        shoot();
    }
}
    //used to fire the bullet
function shoot()
{
   ammoprefab = transform.FindObject("newbullet");
   var ammo = Instantiate(ammoprefab,transform.Find("f2").transform.position, transform.rotation);
   ammo.rigidbody.AddForce(transform.forward *10000);
   Destroy(ammo.gameObject, 1.5); 
}

As you had posted it, the var target in the Update function did nothing, and would be only visible within the Update function, after its declaration within the Update function. However, with this code, I don’t think it should give you a null reference exception, anymore.

However, if it does, or even if it doesn’t, and you come back to post some other null reference questions, later (which is perfectly fine), try double clicking the error itself, within the console window. It will highlight the line on which the null reference exception came up, then paste the whole script, and then, separately, paste the line that the exception was on. It makes helping you a whole lot easier :slight_smile: good luck!

Wow, Destruktorr, Thank you so much for your help, I didnt realize the error had changed with your updated script, my sourcing of the ammoprefab was terrible so after fixing that, everything works now! Perfectly at that! Thank you so much… I would by you a beer if your ever in Colorado heh. Here is the final code for anyone else who needs this sort of thing. All internal variables, turret tracking and firing code. Now to the next script!

private var ammoprefab: GameObject;
private var nextTime : float;
private var target : GameObject;
    //start shooting
function Start () {
    InvokeRepeating ("fire", 0, 0.3);
}
    // Finding the closest target
function find() : GameObject {
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Enemy"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance; 
            } 
    } 
    return closest;    
}
    //used to look at the target
function Update()
{
    target = find();
    transform.LookAt(target.transform);
}
    //used to determine target and distance to shoot at
function fire(){
var range = 500;
if(target)
{
    if(Vector3.Distance(transform.position, target.transform.position) < range)
    {
        shoot();
    }
}
}  //used to fire the bullet
function shoot()
{
   ammoprefab = transform.gameObject.Find("newbullet");
   var ammo = Instantiate(ammoprefab,transform.Find("f2").transform.position, transform.rotation);
   ammo.rigidbody.AddForce(transform.forward *10000);
   Destroy(ammo.gameObject, 1.5); 
}