list and targeting problem.

I’m trying to get a goul (player pets) to switch targets when the current target dies.
I can target the first one in the list, but when it dies I get the “transform has been destroyed but you’re still trying to access it” error. As well as not being able to change to the next target in the list.

can someone please look over the code and guide me in the right direction? :smile:
ps, I’m using the tab button for right now, but eventually the target needs to change automatically.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Goul : MonoBehaviour {
	public Enemy2D enemy2D;
	public Transform target;
	public Transform player;
	public int moveSpeed;
	private Transform goul;
	public int idleDistance = 1;
	public int enemyMaxDistance = 20;
	public int enemyMinDistance = 2;
	//attack vars
	public float attackRange = 2;
	float attackRate = 1f;
	float coolDown = 1;
	public int goulDamage = 1;
	//targeting vars
	public List<Transform> targets;
	public Transform selectedTarget;
	private Transform myTransform;
	//12+ hours on this fucking script...
	void Awake(){
		goul = transform;
	}
	
	void Start () {
		targets = new List<Transform> ();
		selectedTarget = null;
		myTransform = transform;
		AddAllEnemies ();


		//GameObject to = GameObject.FindGameObjectWithTag ("Enemy");
		//target = to.transform;

		GameObject po = GameObject.FindGameObjectWithTag("Player");
		player = po.transform;
		

	}
	public void AddAllEnemies(){
		GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
		
		foreach (GameObject enemy in go)
			AddTarget (enemy.transform);
	}
	public void AddTarget(Transform enemy){
		targets.Add (enemy);
	}
	private void SortTargetsByDistance(){
		if (selectedTarget != null) {
			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) {
			
			SortTargetsByDistance ();
			selectedTarget = targets [0];
		} else {

			int index =targets.IndexOf(selectedTarget);
			if(index < targets.Count -1){
				index++;
			}
			else {
				index = 0;
			}
			DeselectTarget();
			selectedTarget = targets[index];
			
		}
		SelectTarget();
		target = selectedTarget.gameObject.transform;
	}
	//indicates which target you have selected ( next 2)
	private void SelectTarget(){
		if (selectedTarget != null) {
			selectedTarget.renderer.material.color = Color.green;
			
		}
	}
	public void DeselectTarget(){
		selectedTarget.renderer.material.color = Color.red;
		selectedTarget = null;
	}
	
	void Update (){ 
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
		if (Input.GetKeyDown (KeyCode.Tab)) {
			TargetEnemy();		
		}
		
	}
	
	void FixedUpdate () {
		if( target != null){
			if (Vector3.Distance (goul.position, target.position) < attackRange) {
				if (Time.time >= coolDown){
					GoulAttack();
					coolDown = Time.time + attackRate;
				}
			}
		}

		if (target == null || Vector3.Distance (goul.position, target.position) > enemyMaxDistance){
			if (Vector3.Distance (goul.position, player.position) > idleDistance) {
				if (player.position.x < goul.position.x) {
						goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
				} else if (player.position.x > goul.position.x) {
						goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
					  }
				}

		}

		else if (target != null) {
			if (Vector3.Distance (player.position, target.position) < enemyMaxDistance) {
				if (Vector3.Distance (goul.position, target.position) > enemyMinDistance) { 
					if (target.position.x < goul.position.x) {
						goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
					} else if (target.position.x > goul.position.x) {
						goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
					}
				}
			}
		}
		  
						 

		
		Debug.DrawLine(player.position, goul.position, Color.yellow);
		if (target != null) {
						Debug.DrawLine (target.position, goul.position, Color.green);
		}
		
	}
	private void GoulAttack(){
		//find target with tag
		if (target.gameObject.tag == "Enemy") {
			                                          //send damage call to enemy script
						target.gameObject.SendMessage ("EnemyDamaged", goulDamage, SendMessageOptions.DontRequireReceiver);
				}
	}
	
}

Why dont you try first setting the Target=null after you destroy the actuall first enemy so you dont have any transform to access, and then you can try checking the next enemy by onTriggetStay within teh pet radius? you can always try debug.warning to check if theres another actual enemy to target after destroyign the enemy and before making it the new target

Kinda new as well but hopefully can try to help with logic :slight_smile:

  1. do you mean a list that is added to with a trigger? or the pet goes for enemies within the enemies trigger radius. because both are good ideas! never thought of that.
  2. whats debug.warning? i cant find it in the documentation :eyes:

ps, i got the error to go away. i put your code line on the DeselectTarget class. thank you