Find all plants and add their x,y,z in arrays not working. [Solved]
[SerializeField]
public static float[] xPos = new float[500];
public static float[] yPos = new float[500];
public static float[] zPos = new float[500];
public static int Objects;
public void Save()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("SativaPlant");
foreach (GameObject i in objects)
{
xPos[Objects] = i.gameObject.transform.position.x;
yPos[Objects] = i.gameObject.transform.position.y;
zPos[Objects] = i.gameObject.transform.position.z;
Objects++;
}
}
So heres is the code, i have no idea how to make it work. It says that all i.gameObjects have 0 in x,y and z
Comment
i found the solution, it was a bit annoying but finaly got it working :)
public void Save()
{
Objects = 0;
GameObject[] objects = GameObject.FindGameObjectsWithTag("SativaPlant");
Debug.Log(objects.Length);
for (int i = 0; i < objects.Length; i++)
{
xPos[Objects] = objects[i].gameObject.transform.position.x;
yPos[Objects] = objects[i].gameObject.transform.position.y;
zPos[Objects] = objects[i].gameObject.transform.position.z;
Objects++;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612083010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# ArrayList Accessing and RemoveAt? 0 Answers
Another Physics.OverlapSphere question 0 Answers
Getting an error when using an Array of String Arrays 0 Answers
Button locking/unlocking for loop using an object array & playerprefs 1 Answer
Assigning a different material to different objects 2 Answers