Hi, Code (CSharp): 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.
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): GameObject[] array = GameObject.FindGameObjectsWithTag("Player"); foreach(GameObject player in array){ Debug.Log(player.transform.position); }
I believe you could also get the first index of the array directly by doing this: Code (CSharp): GameObject player = GameObject.FindGameObjectsWithTag("Player")[0];
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): if (Vector2.Distance(transform.position, target.position) > attackDistance) { transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); } 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
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): //Adding the square brackets to the variable name declares it as an array. 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): foreach(GameObject player in players) { Debug.Log(player.transform.position); }