updating a list help

So I’m using this as targeting systems. Works fine until an object is deleted from the game such as an enemy when it dies. I tried many things but cant get the list to update it list selection.

public class Targeting : MonoBehaviour {
	
	public List<Transform> targets;
	public Transform selectedTarget;
	private Transform myTransform;
		

	// Use this for initialization
	void Start () {
		targets = new List<Transform>();
		
		addAllEnemies();
		selectedTarget = null;
		myTransform = transform;
		
	}
	
	public void addAllEnemies()
	{
		GameObject [] go = GameObject.FindGameObjectsWithTag("Targetable");
		
		foreach(GameObject Targetable in go)
			addTarget(Targetable.transform);
	}
	
	public void addTarget(Transform Targetable)
	{
	
		targets.Add(Targetable);	
	}
	
	
	private void sortTargetbyDistance()
	{
		targets.Sort(delegate(Transform t1, Transform t2) {
			return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
		});
	}
	
	private void targetEnemy()
	{
		if (selectedTarget == null)
		{
			sortTargetbyDistance();
			selectedTarget = targets[0];
		}
		else
		{
			int index = targets.IndexOf(selectedTarget);
			
			if (index < targets.Count -1)
			{
				index++	;
			}
			else 
			{
				index = 0;
			}
			
			deSelect();
			selectedTarget = targets[index];
				
		}
		selectTarget();
	}
	
	private void  selectTarget()
	{
		//Effect when target gets selected. 
		selectedTarget.renderer.material.color = Color.yellow;
		
		PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
		pa.target = selectedTarget.gameObject;
	}
	
	private void deSelect()
	{
		selectedTarget.renderer.material.color = Color.blue;
		selectedTarget = null;
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.Tab))
		{
			targetEnemy();
			
		}
	}
}

i’m guessing you are copying the burzerg arcade script. why not search on his website about it? if you keep following the tutorials he eventually fixes some of the issues.

I’ll search but i’m not making a game like his. Also you have to follow hundreds of videos just to know if something works later with his scripts. I watched his videos to learn c# not to copy a game. Only thing I’m asking is how to update list in general. I tried many things and can’t get the list to update. I just want the list where it won’t get errors when I delete an object and/or add new ones when come into play.

1 Like

lol you are taking it too seriously i’m trying to help out lol. what i meant was more like since you are watching his tutorials why not follow it all the way? if you just watch one video then realize that it has a problem you will realize that he fixes it on the next one.

Because you never remove a destroyed enemy from the list. Do that and you should be fine.

I tried targets.Remove(Targetable); and targets.RemoveAll(Targetable); along with few other things. I also watched the other videos and his targeting system still doesn’t seem to update, just list mobs at start. This doesn’t help me. BTW I wasn’t trying to be aggressive with last comment. Just stating a point.