Position Array Returning null reference
First time asking a question but i cannot figure it out... I have a script ( below ) that is getting every gameobject with the tag cage and storing them into an array. Next i want it to get there position and store that into another array but i am getting a null reference exeption... In my scene I have 9 cages in total each at different x positions and yes they do have the cage tag and i can debug.log that and the array does list the 9 cages. The problem is when i try to get the positions. here is my code. thanks in advance
private GameObject[] Cages;
private Vector3[] CagesPos;
public void Start()
{
Cages = GameObject.FindGameObjectsWithTag("Cage");
for (int i = 0; i < Cages.Length; i++)
{
Cages[i].transform.position = CagesPos[i]; //this is the line throwing null reference
}
}
Please help!!
Answer by Positive7 · Sep 15, 2015 at 12:58 AM
If you want to save the value to CagesPos shouldn't be CagesPos[i] = Cages[i].transform.position
; instead Cages[i].transform.position = CagesPos[i];
private GameObject[] Cages;
private Vector3[] CagesPos;
public void Start()
{
Cages = GameObject.FindGameObjectsWithTag("Cage");
CagesPos = new Vector3[Cages.Length];
for (int i = 0; i < Cages.Length; i++)
{
//Cages[i].transform.position = CagesPos[i];
CagesPos[i] = Cages[i].transform.position;
}
}
Perfect!! haha its been a long day... i should of had CagesPos[i] = Cages[i].transform.position;
but the real thing i forgot about and didn't even know i need was CagesPos = new Vector3[Cages.Length];
thank you!!!!!