Shooter script throw Null reference exception despite I filled both fields exposed in inspector
public class shooter : MonoBehaviour
{
[SerializeField] GameObject projectile;
[SerializeField] GameObject gun;
AttackerSpawner myLaneSpawner;
Animator animator;
// Start is called before the first frame update
private void Start()
{
setLaneSpawner();
animator = GetComponent<Animator>();
}
private void Update()
{
if (IsAttackerInLane())
{
animator.SetBool("IsAttacker", true);
}
else
{
animator.SetBool("IsAttacker", false);
}
}
private void setLaneSpawner()
{
AttackerSpawner [] spawners = FindObjectsOfType<AttackerSpawner>();
foreach(AttackerSpawner spawner in spawners)
{
bool isCloseEnough = (Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);
if (isCloseEnough)
{
myLaneSpawner = spawner;
}
}
}
private bool IsAttackerInLane()
{
if(myLaneSpawner.transform.childCount <= 0)
{
return false;
}
else
{
return true;
}
}
public void Fire()
{
Instantiate(projectile,gun.transform.position, Quaternion.identity);
}
}