Get Gameobject Element from List in other Script and place it in current Script
Hey. So, i have a gameobject CameraRig with a Script CameraControl.cs and a Game.cs Script
in the CameraRig is this variable:
public GameObject[] m_Spawnpoints;
and i want to put in my Game.cs the var
public GameObject Spawnpoint1;
for this i wanted get the first Element of the m_Spawnpoints List from the CameraRig list like this:
GameObject.Find("CameraRig").GetComponent<CameraControl>().m_Spawnpoints[1] = Spawnpoint1;
and put it in the Spawnpoint1 . but the Script above doesent work, why? this seams to be also wrong
Spawnpoint1 = GameObject.Find("CameraRig").GetComponent<CameraControl>().m_Spawnpoints[1]:
thanks!
Answer by wibble82 · Nov 13, 2015 at 03:23 PM
Hi
I'm going to assume that 'm_Spawnpoints' has been allocated, either by setting it up the inspector or some other code.
In that case I'd suggest the problem is you are accessing 'm_Spawnpoints[1]', but arrays start at 0. So the first element would be m_Spawnpoints[0].
To copy the value in the first element of the m_Spawnpoints array into Spawnpoint1, you would do:
Spawnpoint1 = GameObject.Find("CameraRig").GetComponent<CameraControl>().m_Spawnpoints[0];
I'm assuming that's your problem. I'll need more info than 'doesnt work' to help further :)
Your answer
