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
}
}