I’m having an issue with my script not finding a gameobjects position, and I believe it’s how I’m setting it but I’m not quite sure how to do it properly. First, here are my two declarations. Please note that the players variable is set using GetAllGameObjectsWithTag(“Players”)
GameObject[] players;
public Vector3[] _pos;
…
for (int x = 0; x < players.Length; x++)
{
players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
players[x].GetComponent<Animator>().SetBool("MovingRight", false);
_pos[x] = players[x].gameObject.transform.position;
}
This first two lines that set the bool work just fine, so I’ve got the right game objects, but no matter how I set the _pos, it always errors saying it can’t find it. How do I set the position to the corresponding game object?(Also, I really only need the ‘x’ value of position though either way is fine)
Marceta
2
If I understood you correctly you want to collect positions of all players. You could do it like this:
GameObject[] players;
public List<Vector3> _pos = new List<Vector3>();
for (int x = 0; x < players.Length; x++)
{
players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
players[x].GetComponent<Animator>().SetBool("MovingRight", false);
_pos.Add(players[x].gameObject.transform.position);
}