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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Please help about "FindWithTag" and "Array" sir.

Discussion in 'Getting Started' started by Pollawat, Aug 31, 2018.

  1. Pollawat

    Pollawat

    Joined:
    Aug 27, 2016
    Posts:
    192
    Hello sir, I'm a newbie and here is my problem.

    I try to make a group of player that enemy can detect whoever go inside their sight first, they will attack. So, I started to write a code that the enemy can detect the closest player.

    Code (CSharp):
    1.  
    2.  public class AI_SneakingDetectZone : MonoBehaviour
    3.     {
    4.         public float DistanceToPlayer = 20;
    5.         public float AngleToDetect = 180;
    6.  
    7.  
    8.         public Transform[] PlayerList;
    9.         public GameObject closest;
    10.  
    11.  
    12.         void Start()
    13.         {
    14.             GameObject[] PlayerObject = GameObject.FindGameObjectsWithTag("Player");
    15.             PlayerList = new Transform[PlayerObject.Length];
    16.  
    17.             for (int i = 0; i < PlayerList.Length; ++i)
    18.                 PlayerList[i] = PlayerObject[i].transform;
    19.         }
    20.  
    21.  
    22.         void Update()
    23.         {
    24.  
    25.             float DistanceValue = Vector3.Distance(transform.position, PlayerList[0].position);
    26.  
    27.             for (int i = 0; i < PlayerList.Length; i++)
    28.             {
    29.                 float CurrentDistance = Vector3.Distance(transform.position, PlayerList[i].position);
    30.                 if (CurrentDistance < DistanceValue)
    31.                 {
    32.                     DistanceValue = CurrentDistance;
    33.                     closest = PlayerList[i].gameObject;              
    34.                 }
    35.  
    36.             }
    37.             /*      
    38.             {
    39.                 Debug.Log("Something");
    40.             }    
    41.            */
    42.         }
    43.  
    44.     }
    45.  
    Edit. now I got this far, it's working fine but I don't know why for some reason "element 0" is not being detected as the closest object like the other, whatever what inside of it.



    For example, in this picture "H6" never gonna set place in Closest target, or whatever object that sits into "Element 0" will not work correctly.
     
    Last edited: Aug 31, 2018
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Please don't assume that everyone here is male.

    Code (csharp):
    1.  
    2.             float DistanceValue = Vector3.Distance(transform.position, PlayerList[0].position);
    3.  
    This sets
    DistanceValue
    to the distance to player 0 (but does not set
    closest
    ).

    Code (csharp):
    1.  
    2.             for (int i = 0; i < PlayerList.Length; i++)
    3.             {
    4.                 float CurrentDistance = Vector3.Distance(transform.position, PlayerList[i].position);
    5.                 if (CurrentDistance < DistanceValue)
    6.                 {
    7.                     DistanceValue = CurrentDistance;
    8.                     closest = PlayerList[i].gameObject;          
    9.                 }
    10.             }
    11.  
    ...and this loop looks for player whose distance is less than that, and sets
    closest
    (as well as DistanceValue). So consider player 0. Is player 0's distance less than player 0's distance (which you set above, before the loop)? No, it is not. So it will never set
    closest
    to player 0.

    The solution is to either change < to <=, or up above the loop where you first set
    DistanceValue
    , also set
    closest
    to PlayerList[0].
     
  3. Pollawat

    Pollawat

    Joined:
    Aug 27, 2016
    Posts:
    192
    Yes, sorry I didn't think that far, I just want to be polite.

    Anyway, this works !!! Thank you very much.
     
    JoeStrout likes this.