Search Unity

How can I change Selected Target???? HELP ME!!!

Discussion in 'Scripting' started by sourna, Oct 29, 2014.

  1. sourna

    sourna

    Joined:
    Aug 29, 2013
    Posts:
    14
    Hi guys!
    when “selectedTarget” gets null value,compiler displays a missing error.

    what I want is is that when “curHelath” in “AddJustCurrentHealth” class gets 0 as a value, “selectedTarget” picks another element which “curHelath” in that has a non-zero value.
    Capture1111.JPG
    Capture1112.JPG
    Capture1114.JPG
    Capture1115.JPG

    class EnemyHealth

    Code (CSharp):
    1. public class EnemyHealth : MonoBehaviour {
    2. public int curHelath = 100;
    3.  
    4. public void AddJustCurrentHealth (int adj)
    5.     {
    6.  
    7.         curHelath +=adj;
    8.  
    9.         if(curHelath<=0)
    10.         {
    11.             curHelath =0;
    12.             boss.enabled =  false;
    13.             Destroy(gameObject,timeDes);
    14.             //Targetting des = (Targetting)GetComponent("Targetting");
    15.             //des.RemoveTarget(enemy2:transform);
    16.             //LevelReset();
    17.  
    18.         }
    19. }
    class Targetting

    Code (CSharp):
    1. public class Targetting : MonoBehaviour
    2. {
    3.     public List<Transform> targets;
    4.     public Transform selectedTarget;
    5.     float timeDes =4f;
    6.     private Transform myTransform;
    7.     private void TargetEnemy()
    8.     {
    9.  
    10.         if (selectedTarget == null)
    11.         {
    12.             //SortTargetsByDistance();
    13.             selectedTarget = targets[0];
    14.         }
    15.         else
    16.         {
    17.             int index = targets.IndexOf(selectedTarget);
    18.            
    19.             if(index < targets.Count-1)
    20.             {
    21.                 index++;
    22.             }
    23.             else
    24.             {
    25.                 index = 0;
    26.             }
    27.  
    28.             selectedTarget = targets[index];
    29.        
    30.  
    31.         }
    32.  
    33.             SelectTarget();
    34.  
    35.     }
    36.  
    37.  
    38.     private void SelectTarget()
    39.     {  
    40.         //selectedTarget.renderer.material.color= Color.green;
    41.  
    42.         CharacterAttack character = (CharacterAttack)GetComponent("CharacterAttack");
    43.         character.target = selectedTarget.gameObject;
    44.  
    45.  
    46.     }
    47. void Update ()
    48.     {
    49.         targets.RemoveAll(list_item => list_item == null);
    50.  
    51.         if(targets!=null)
    52.         {
    53.  
    54.             CharacterAttack character2 = (CharacterAttack)GetComponent("CharacterAttack");
    55.             float distance = Vector3.Distance(character2.target.transform.position,transform.position);
    56.        
    57.             if(distance <= 1f)
    58.             {
    59.  
    60.                     TargetEnemy();
    61.             }
    62.         }
    63.     }    
    64. }
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    When your target is destroyed, you attempt to check the distance between the player and the target. This is where you have an issue. You could throw a null check before the distance calculation. If target is null, run your TargetEnemy method again.

    I prefer to use eventing for this sort of thing. My units (enemy and ally) all throw an OnDeath event when they die, before they are destroyed. My targetting script would subscribe to this event, and run a method that will remove the enemy from my list of targets and select a new target for me. Everything is automatic.

    Edit: Also, cache your CharacterAttack component. No need to GetComponent on that once, possibly twice, every update call.
     
  3. sourna

    sourna

    Joined:
    Aug 29, 2013
    Posts:
    14
    help me ,how can write script :(:(:(
     
  4. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Right on Zaladaur

    When your enemy/target dies remove its reference from your target list & clear your current target before you destroy the object. This way when you check for the next target it will not encounter a null reference. This needs to be called from your enemy script right before you call your destroy command.

    Make sure as long as you have no target you are not executing any code that relies on a target with the use of "null" checks.