I cant fix one error, please help

I have this error message I dont know how to fix it . Thanks for any help guys.

NullReferenceException: Object reference not set to an instance of an object
Shooter.IsAttackerAheadInLane () (at Assets/Scripts/Deffenders/Shooter.cs:53)
Shooter.Update () (at Assets/Scripts/Deffenders/Shooter.cs:28)

Thats my Shooter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooter : MonoBehaviour {  //defenders striela aj ma spawner nejake child ak nie tak nestrielam

public GameObject projectiles, gun;

private GameObject projectileParent;
private Animator animator;
private Spawner myLaneSpawner;

void Awake(){
    animator = GameObject.FindObjectOfType<Animator>();
    SetMyLaneSpawner();
}

void Start(){
    projectileParent = GameObject.Find("Projectiles"); //najdem objekt Projectiles 

    if (!projectileParent) //ak neexistuje ako rodic objektu projectilu
    {
        projectileParent = new GameObject("Projectiles"); //tak mu ho pridelim
    }
}

void Update(){
    if(IsAttackerAheadInLane()){
        animator.SetBool("isAttacking Bool",true);
    }else{
        animator.SetBool("isAttacking Bool",false);
    }
}


void SetMyLaneSpawner(){
    Spawner[] spawnerArray = GameObject.FindObjectsOfType<Spawner>();
    foreach (Spawner spawner in spawnerArray)
    {
        if (spawner.transform.position.y == transform.position.y) //ak sa nachadza deffender sa rovnakej pozicii ako spavner tak idem dalej
        {
            myLaneSpawner = spawner; // preberem aktualny spawner do premennej moj spawner
            return;
        }   
    }

    Debug.LogError(name + "cant find spawner in lane");

}

bool IsAttackerAheadInLane(){
    //Exit ak niesu attackers in lane
    if(myLaneSpawner.transform.childCount <= 0){
        return false;
    }

    //ak su tam deffendery tak ci su na mojej urovni
    foreach (Transform attacker in myLaneSpawner.transform)
    {
        if (attacker.transform.position.x > transform.position.x)
        {
            return true;
        }
    }

    //su attackery ale za nami uz 
    return false;

}


private void Fire(){
    GameObject newProjectiles = Instantiate(projectiles) as GameObject; //vytvorenie a odchytenie projectilu
    newProjectiles.transform.parent = projectileParent.transform; //presunutie projektilu pod gameobject Projectiles
    newProjectiles.transform.position = gun.transform.position; // novy projectil dat na poziciu gun objektu na deffenderovi
}

}
1 Like

Seems that the error is when you are adressing the value to the spawner

if (spawner.transform.position.y == transform.position.y)
{
myLaneSpawner = spawner; // preberem aktualny spawner do premennej moj spawner
return;
}

Try printing something before the return to make sure it is actually working

I have the same error and couldnt find a solution .

Hello there,
Could you paste your code somewhere? Either here or using pastebin/codeshare.

Lizard and fox was not appearing under spawners. Probably that makes the error . I got the codes from github from bens page and solved. Need to study harder i guess🙂thankss

1 Like

I have this problem and I can’t seem to fix it. The error I get is this

NullReferenceException: Object reference not set to an instance of an object
Shooter.IsAttackerAheadInLane () (at Assets/Scripts/Shooter.cs:47)
Shooter.Update () (at Assets/Scripts/Shooter.cs:23)

My Shooter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooter : MonoBehaviour {

    public GameObject projectile, gun;
    private GameObject projectileParent;
    private Animator animator;
    private Spawner myLaneSpawner;
	
	void Start() {
        animator = GetComponent<Animator>();

    // Creates a parent if necessary
        projectileParent = GameObject.Find("Projectiles");
        if (!projectileParent) {
            projectileParent = new GameObject("Projectiles");
        }
	}
	
	void Update() {
		if (IsAttackerAheadInLane()) {
            animator.SetBool("isAttacking", true);
        } else {
            animator.SetBool("isAttacking", false);
        }

        SetMyLaneSpawner();
        print(myLaneSpawner);
	}

    void SetMyLaneSpawner() {
        Spawner[] spawnerArray = GameObject.FindObjectsOfType<Spawner>();

        foreach (Spawner spawner in spawnerArray) {
            if (spawner.transform.position.y == transform.position.y) {
                myLaneSpawner = spawner;
                return;
            }
        }
        Debug.LogError(name +  " cannot find spawner in lane");
    }

    bool IsAttackerAheadInLane() {
        // Exit if no attackers in lane
        if (myLaneSpawner.transform.childCount <= 0) {
            return false;
        }

        // If there are attackers, are they ahead?
        foreach (Transform attacker in myLaneSpawner.transform) {
            if (attacker.transform.position.x > transform.position.x) {
                return true;
            }
        }

        // Attackers in lane but not ahead
        return false;
    }

    private void Fire() {
        GameObject newProjectile = Instantiate(projectile) as GameObject;
        newProjectile.transform.parent = projectileParent.transform;
        newProjectile.transform.position = gun.transform.position;
    }

}

I managed to fix it just minutes after posting haha. What I did was in the Start() method, I added SetMyLaneSpawner() in it.

Privacy & Terms