Every spawner’s assigned as an AttackerSpawner in the Inspector. I’m now not so sure what I missed. Is there something in my code that needs to be fixed? Or something in the inspector that needs to be done?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
[SerializeField] GameObject projectile;
[SerializeField] GameObject shootPos;
AttackerSpawner myLaneSpawner;
Animator animator;
public void Start()
{
SetLaneSpawner();
animator = GetComponent<Animator>();
}
public void Update()
{
if (IsAttackerInLane())
{
animator.SetBool("IsAttacking", true);
} else
{
animator.SetBool("IsAttacking", 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, shootPos.transform.position, Quaternion.identity);
}
}