- Home /
Keep adding targets to a list
Hello, I have a list on witch i have every enemy on the map sorted by its distance to the tower (it's a tower defense game) The problem is in the method i use to add those targets to the list, I can only use it once because i dont know how to clear the array inside it.
i've tried using ArrayName.Clear(); with no results, i can't either get the lenght of it i guess because it's a GameObject Array but not sure...
public void AddAllEnemies(){
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go){
AddTarget(enemy.transform);
i++;
}
}
public void AddTarget(Transform enemy){
targets.Add(enemy);
}
This is the most important part of the code witch i'll post at the end, when an enemy dies i am supposed to clear the list and then add every enemy on the map but when i do this, because i can't clear the "go" array i add the actual enemys and the previous ones, so i end up with the same list twice, but the main problem is that the enemy i killed is dead and therefor when i sort them by distance an error appears.
Bottom line i need to clear the previous GameObject array everytime i call the AddAllEnemies method or find a way to update my "targets" list.
Thanks to everyone who tries to help me, and i hope you can because i am a bit despered with this S:
using UnityEngine; using System.Collections; using System.Collections.Generic; //using System;
public class TowerStone : MonoBehaviour {
public List targets;
public GameObject projectile;
public Transform target;
private Transform myTransform;
private float coolDown = 2;
private float t;
private int i = 0;
private bool ready = true;
private bool arrayOn = false;
//private delegate Transform myDelegate(Transform t1, Transform t2);
// Use this for initialization
void Start () {
myTransform = transform;
float t = Time.time;
AddAllEnemies();
TargetEnemy();
}
// Update is called once per frame
void Update () {
TargetEnemy();
float c = Time.time - t;
if (c > coolDown && ready){
t = Time.time;
Fire();
}
/*if(Input.GetKeyDown(KeyCode.Y)){
AddAllEnemies();
}*/
}
void Fire(){
GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject;
arrow.SendMessage("SetTarget", target);
}
public void AddAllEnemies(){
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go){
AddTarget(enemy.transform);
i++;
}
}
private void SortTargetsByDistance(){
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position));
});
}
public void AddTarget(Transform enemy){
targets.Add(enemy);
}
private void TargetEnemy(){
SortTargetsByDistance();
target = targets[0];
ready = true;
}
private void TargetDestroyed(){
targets.RemoveAt(0);
ready = false;
SortTargetsByDistance();
print (target);
}
void AddMe(){
}
}
Answer by Seth-Bergman · Aug 11, 2012 at 07:46 PM
if I understand, you want to clear the GameObject[] "go", correct?
the line:
GameObject[] go = new GameObject[];
or something like that, should clear it, so maybe like:
GameObject[] go = new GameObject.FindGameObjectsWithTag("Enemy");
not sure if it will work this easily though..
@Flague: Seth is right, you could just create a new array which would then be empty. You could also just iterate through the array and pop them out of the array.
foreach(GameObject enemy in go){ go.Pop(); }
Yes, = new GameObject[] will clear it. But you should remove 'new' from the second line - new is just for when you're creating the new array yourself. Since FindBlahBlah creates the array,l you don't need 'new.'
Answer by Flague · Aug 12, 2012 at 06:05 PM
Thanks for the answers, i solved it by declaring the array null every time i call the AddAllTargets method, the "Problem" is that i call it every frame on the update, can't just call it when one target dies don't know why but it doesn't matter even thought i think calling it every frame will make it laggy but meh, here is the code if anyone is interested (:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : MonoBehaviour {
private float coolDown = 2;
private float t;
public List targets;
public Transform target;
public GameObject projectile;
float dist = 100;
float distToFire = 5;
void Start () {
AddAllTargets();
}
void Update () {
AddAllTargets();
if (target != null)
if(dist < distToFire)
Timing();
}
void AddAllTargets(){
GameObject[] go = null;
go = GameObject.FindGameObjectsWithTag("Enemy");
targets.Clear();
for(int i = 0; i < go.Length; i++){
}
foreach (GameObject enemy in go){
targets.Add(enemy.transform);
}
SortTargetsByDistance();
TargetEnemy();
dist = Vector3.Distance(transform.position, target.position);
}
private void TargetEnemy(){
if (targets.Count > 0){
target = targets[0];
}
}
private void SortTargetsByDistance(){
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position));
});
}
void Fire(){
GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject;
arrow.SendMessage("SetTarget", target);
}
private void Timing(){
float c = Time.time - t;
if (c > coolDown){
t = Time.time;
if (target != null)
Fire();
}
}
}
is the for loop in the AddAllTargets() method still necessary? I have a code similar, and I am trying to solve a problem I have for my own script.
Your answer
Follow this Question
Related Questions
How to remove objects from a list ? 3 Answers
Can't add GameObjects to ArrayList 1 Answer
How do I check multiple gameObjects transform positions? 3 Answers
Disable All Transform In List 1 Answer