Search Unity

enemy ai player finder

Discussion in '2D' started by Helladah, Sep 2, 2018.

  1. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    228
    Hi,
    Code (CSharp):
    1. GameObject.FindGameObjectsWithTag("Player").transform
    tells me that " ' GameObject[]' dosent contain a definition of 'transform'... " any helps? Or any way to do waht this script that i found tries to do? i mean, it seems prety cool.
     
  2. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    20
    Function
    GameObject.FindGameObjectsWithTag()
    returns an array. You are trying to get transform of an array not a gameobject. You need to save array to a variable and then iterate through it. You might want to try something like this:
    Code (CSharp):
    1. GameObject[] array = GameObject.FindGameObjectsWithTag("Player");
    2. foreach(GameObject player in array){
    3.    Debug.Log(player.transform.position);
    4. }
     
    Vryken likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    772
    I believe you could also get the first index of the array directly by doing this:
    Code (CSharp):
    1. GameObject player = GameObject.FindGameObjectsWithTag("Player")[0];
     
    Helladah likes this.
  4. lipisis

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    20
    Yes, you can.
     
    Helladah likes this.
  5. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    228


    I dont understand how to get the transform info from the array :/, shall i declare a new vector, and use it instead the "player.position"?

    Code (CSharp):
    1. if (Vector2.Distance(transform.position, target.position) > attackDistance)
    2.  
    3.         {
    4.  
    5.  
    6.  
    7.             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    8.  
    9.  
    10.  
    11.         }

    And if i have two characters at screen, or more than one playable character to select, would it works? or just for the first saved at array
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    772
    It would get the index you specify. "[0]" is the first element in the array, "[1]" is the second element, "[2]" is the third element, and so on.

    If you are 100% certain that there will only be one player, then you could use what I posted above (though there are better ways than using FindGameObjectsWithTag in that case).

    To store an array of values, all you have to do is add square brackets to your declaration:
    Code (CSharp):
    1. //Adding the square brackets to the variable name declares it as an array.
    2. GameObject players[] = GameObject.FindGameObjectsWithTag("Player");
    And say, for example, you want to print out the position of each player in the scene, then you'd iterate through the array in a loop like what @lipisis posted:
    Code (CSharp):
    1. foreach(GameObject player in players) {
    2.    Debug.Log(player.transform.position);
    3. }
     
    Helladah likes this.