- Home /
1 out of 4 objects with the same script that reference the same transform list returning null?
Hi, i have 4 Gameobjects (turrets) with this same script attached which use a list of Transforms as targets. The Coroutine TargetSystem first checks if the list has a count > 0 "if (etm.targets.Count > 0)" or else "lockedBlocked = null; StopCoroutine(TargetSystem());" etc.
The issue im having is 3 out of the 4 turrets are passing the if count > 0 and 1 is returning the else = null?!
public class TurretController : MonoBehaviour
{
private EnemyTargetManager etm;
[HideInInspector] public Transform lockedBlock = null;
public XRLineRenderer beam1;
public float fireDistance = 300.0f;
public float rayDistance = 300.0f;
public float turnSpeed = 50.0f;
private Quaternion targetRot;
private bool targetAcquired = false;
private ShipBlockColl blockColl;
private AudioSource source;
private void Start()
{
etm = FindObjectOfType<EnemyTargetManager>();
etm = etm.GetComponent<EnemyTargetManager>();
StartCoroutine(TargetSystem());
source = GetComponent<AudioSource>();
}
private void Update()
{
if (targetAcquired == true && lockedBlock != null)
{
targetRot = Quaternion.LookRotation(lockedBlock.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, turnSpeed * Time.deltaTime);
}
}
IEnumerator TargetSystem()
{
//print("target started!");
if (etm.targets.Count > 0)
{
//print("found target");
if (etm.targets[0].CompareTag("ShipBlock"))
{
lockedBlock = etm.targets[Random.Range(0, etm.targets.Count)];
Vector3 blockDistance = lockedBlock.position;
float dist = Vector3.Distance(transform.position, blockDistance); //Debug.Log(dist);
if (Vector3.Distance(transform.position, blockDistance) < fireDistance)
{
targetAcquired = true;
yield return new WaitForSeconds(1f);
StartCoroutine(FireBeam());
yield return new WaitForSeconds(5f);
targetAcquired = false;
yield return StartCoroutine(TargetSystem());
}
else
{
yield return new WaitForSeconds(1f);
StartCoroutine(TargetSystem());
}
}
}
else
{
lockedBlock = null; // <------o but why?
StopCoroutine(TargetSystem());
print("LockedBlock Null!");
yield return null;
}
if (etm.targets.Count < 0)
{
print("No target Count!");
StopCoroutine(TargetSystem());
lockedBlock = null;
yield return null;
}
}
IEnumerator FireBeam()
{
//print("Beam Fired!");
source.pitch = 0.9f;
source.Play();
yield return new WaitForSeconds(0.80f);
beam1.enabled = true;
CastBeam();
yield return new WaitForSeconds(2.5f);
source.Stop();
beam1.enabled = false;
yield return null;
}
public void CastBeam()
{
RaycastHit hit;
Vector3 direction = transform.forward;
LayerMask mask = LayerMask.GetMask("ShipBlock");
if (Physics.Raycast(transform.position, direction, out hit, rayDistance, mask))
{
//print("ray fired!");
ShipBlockColl blockColl = hit.collider.GetComponent<ShipBlockColl>();
//Debug.DrawRay(transform.position, direction * rayDistance, Color.red, 2f);
if (blockColl != null && hit.collider.CompareTag("ShipBlock"))
{
//print("beam hit!");
blockColl.RayBeamHit();
}
else
{
Debug.Log("Did not Hit");
}
}
}
}
I have found a dirty workaround by copying and renaming the script just for this one turret and adding this to the else statement............
else
{
lockedBlock = null; // <------o but why?
StopCoroutine(TargetSystem());
print("LockedBlock Null!");
yield return new WaitForSeconds(2f);
StartCoroutine(TargetSystem());
}
After the Coroutine is started again it works fine :/
thanks.
Answer by Frazzle402 · Jun 14, 2020 at 01:47 AM
Fixed by myself by initializing my Transform list of targets in Awake instead of Start, i think the 4th turrets TargetSystem coroutine was running before the list was initializing hence the return to else = null.