- Home /
Issue Sorted By Poster
Add GameObjects to a list using OnTriggerEnter
Making a simple TD game but i'm having trouble with the order that the turrets i have shoot the targets. I want to add each targer to a list as each one moves into the attack radius of the turret but i'm not sure how i would do this. This is my code so far:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Turret : ClickToPlace { [SerializeField] private List<GameObject> m_aEnemys = new List<GameObject>(); [SerializeField] private GameObject m_SpikeSpawn; [SerializeField] private GameObject m_SpikePrefab; private EnemyMove m_Player; // Update is called once per frame void Update() { //If the player is near then the turret head follows the player if (m_Player != null) { Vector3 vDir = (m_Player.transform.position - transform.position).normalized; transform.forward = vDir; } } /// <summary> /// If the player is near start the Fire coroutine /// </summary> /// <param name="other">checks the Other object not its self</param> protected virtual void OnTriggerEnter(Collider other) { if (m_Player == null) { m_Player = other.GetComponent<EnemyMove>(); StartCoroutine(Fire()); } } /// <summary> /// If the player is not near then call the ResetRotation coroutine /// </summary> /// <param name="other">checks the Other object not its self</param> void OnTriggerExit(Collider other) { if (m_Player != null && m_Player.transform == other.transform) { m_Player = null; } } /// <summary> /// Tells the turret to return to its original possision slowly when called /// </summary> /// <returns></returns> private IEnumerator ResetRotation() { while (Quaternion.Angle(transform.localRotation, Quaternion.identity) > 0.1f) { transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.identity, Time.deltaTime * 10); yield return new WaitForEndOfFrame(); } } /// <summary> /// Tells the turret to wait one second then start firing a projectile in a straight line from the barrel of the turret /// </summary> /// <returns></returns> private IEnumerator Fire() { while (m_Player != null) { yield return new WaitForSeconds(m_fFireRate); if (m_Player != null) { Instantiate(m_SpikePrefab, m_SpikeSpawn.transform.position, m_SpikeSpawn.transform.rotation); } } } }
All help is appreciated :')
protected virtual void OnTriggerEnter(Collider other)
{
m_aEnemys.add(other.gameObject);
//....
}
It keeps telling me that it cant resolve symbol add? Edit $$anonymous$$y bad, the A had to capitalised, that worked thanks!, while this is still open is there a way now remove them from the list when they leave the area or are destroyed?
You can use OnTriggerExit() method to know when it exists the area, but I don't know how you can be notified of the destruction of the object. You can use the Remove method on the list to remove an element.
What $$anonymous$$iraSensei said ^ You might want to take a look at http://www.dotnetperls.com/list
$$anonymous$$aybe before using the game object, you can check with GameObject.Find(...) if it still exists.
Follow this Question
Related Questions
Serialization Error 1 Answer
Lists, Arrays and c# equivilent of js Array() 1 Answer