Fill [] array with all available Objects onAwake?
Hey! so i have this multitrack camera with a
public Transform[] m_Targets; // All the targets the camera needs to encompass.
which is there to locate all possible targets. at the moment the on Awake part looks like this:
private void Awake ()
{
m_Targets[0] = GameObject.Find("Spawn1").transform;
m_Targets[1] = GameObject.Find("Spawn2").transform;
m_Targets[2] = GameObject.Find("Spawn3").transform;
m_Targets[3] = GameObject.Find("Spawn4").transform;
}
that means that i assume that i have 4 Palyers (Spawn1-4 are the spawnpoints that the multitrack cam should follow)
what i want to do now, is that the game checks on awake how many Players are in the game. (They get instantiated at the beginning btw with the name Spawn1-4 and the tag "Player". It can also be that only spawn1 and spawn4 are there) and fill them in the array.
Comment
Best Answer
Answer by OncaLupe · Dec 29, 2015 at 10:18 PM
GameObject[] m_TargetsGO = GameObject.FindGameObjectsWithTag("Player");
m_Targets = new Transform[m_TargetsGO.Length];
for(int i = 0; i < m_TargetsGO.Length; ++i)
{
m_Targets[i] = m_TargetsGO[i].transform;
}
http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
Edit: Missed your array was Transforms, added code to handle this since the method returns GameObjects.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054223im_/https://answers.unity.com/themes/thub/images/avi.jpg)