Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

list and targeting problem.

Discussion in 'Scripting' started by 2dfruity, May 18, 2014.

  1. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    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? :D
    ps, I'm using the tab button for right now, but eventually the target needs to change automatically.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class Goul : MonoBehaviour {
    5.     public Enemy2D enemy2D;
    6.     public Transform target;
    7.     public Transform player;
    8.     public int moveSpeed;
    9.     private Transform goul;
    10.     public int idleDistance = 1;
    11.     public int enemyMaxDistance = 20;
    12.     public int enemyMinDistance = 2;
    13.     //attack vars
    14.     public float attackRange = 2;
    15.     float attackRate = 1f;
    16.     float coolDown = 1;
    17.     public int goulDamage = 1;
    18.     //targeting vars
    19.     public List<Transform> targets;
    20.     public Transform selectedTarget;
    21.     private Transform myTransform;
    22.     //12+ hours on this F***ing script...
    23.     void Awake(){
    24.         goul = transform;
    25.     }
    26.    
    27.     void Start () {
    28.         targets = new List<Transform> ();
    29.         selectedTarget = null;
    30.         myTransform = transform;
    31.         AddAllEnemies ();
    32.  
    33.  
    34.         //GameObject to = GameObject.FindGameObjectWithTag ("Enemy");
    35.         //target = to.transform;
    36.  
    37.         GameObject po = GameObject.FindGameObjectWithTag("Player");
    38.         player = po.transform;
    39.        
    40.  
    41.     }
    42.     public void AddAllEnemies(){
    43.         GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
    44.        
    45.         foreach (GameObject enemy in go)
    46.             AddTarget (enemy.transform);
    47.     }
    48.     public void AddTarget(Transform enemy){
    49.         targets.Add (enemy);
    50.     }
    51.     private void SortTargetsByDistance(){
    52.         if (selectedTarget != null) {
    53.             targets.Sort (delegate(Transform t1, Transform t2) {
    54.                 return Vector3.Distance (t1.position, myTransform.position).CompareTo (Vector3.Distance (t2.position, myTransform.position));
    55.             });
    56.            
    57.            
    58.         }
    59.     }
    60.     private void TargetEnemy(){
    61.        
    62.         if (selectedTarget == null) {
    63.            
    64.             SortTargetsByDistance ();
    65.             selectedTarget = targets [0];
    66.         } else {
    67.  
    68.             int index =targets.IndexOf(selectedTarget);
    69.             if(index < targets.Count -1){
    70.                 index++;
    71.             }
    72.             else {
    73.                 index = 0;
    74.             }
    75.             DeselectTarget();
    76.             selectedTarget = targets[index];
    77.            
    78.         }
    79.         SelectTarget();
    80.         target = selectedTarget.gameObject.transform;
    81.     }
    82.     //indicates which target you have selected ( next 2)
    83.     private void SelectTarget(){
    84.         if (selectedTarget != null) {
    85.             selectedTarget.renderer.material.color = Color.green;
    86.            
    87.         }
    88.     }
    89.     public void DeselectTarget(){
    90.         selectedTarget.renderer.material.color = Color.red;
    91.         selectedTarget = null;
    92.     }
    93.    
    94.     void Update (){
    95.         Vector3 theScale = transform.localScale;
    96.         theScale.x *= -1;
    97.         transform.localScale = theScale;
    98.         if (Input.GetKeyDown (KeyCode.Tab)) {
    99.             TargetEnemy();     
    100.         }
    101.        
    102.     }
    103.    
    104.     void FixedUpdate () {
    105.         if( target != null){
    106.             if (Vector3.Distance (goul.position, target.position) < attackRange) {
    107.                 if (Time.time >= coolDown){
    108.                     GoulAttack();
    109.                     coolDown = Time.time + attackRate;
    110.                 }
    111.             }
    112.         }
    113.  
    114.         if (target == null || Vector3.Distance (goul.position, target.position) > enemyMaxDistance){
    115.             if (Vector3.Distance (goul.position, player.position) > idleDistance) {
    116.                 if (player.position.x < goul.position.x) {
    117.                         goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
    118.                 } else if (player.position.x > goul.position.x) {
    119.                         goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
    120.                       }
    121.                 }
    122.  
    123.         }
    124.  
    125.         else if (target != null) {
    126.             if (Vector3.Distance (player.position, target.position) < enemyMaxDistance) {
    127.                 if (Vector3.Distance (goul.position, target.position) > enemyMinDistance) {
    128.                     if (target.position.x < goul.position.x) {
    129.                         goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
    130.                     } else if (target.position.x > goul.position.x) {
    131.                         goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
    132.                     }
    133.                 }
    134.             }
    135.         }
    136.          
    137.                          
    138.  
    139.        
    140.         Debug.DrawLine(player.position, goul.position, Color.yellow);
    141.         if (target != null) {
    142.                         Debug.DrawLine (target.position, goul.position, Color.green);
    143.         }
    144.        
    145.     }
    146.     private void GoulAttack(){
    147.         //find target with tag
    148.         if (target.gameObject.tag == "Enemy") {
    149.                                                       //send damage call to enemy script
    150.                         target.gameObject.SendMessage ("EnemyDamaged", goulDamage, SendMessageOptions.DontRequireReceiver);
    151.                 }
    152.     }
    153.    
    154. }
     
  2. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    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 :)
     
  3. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    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 o_O

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