fill public Transform[] on Awake?
hey! i want to fill in some Target Transform Dynamically
public Transform[] m_Targets; // All the targets the camera needs to encompass.
so it should look on Awake for all Spawn Points available and fill it in the array. I did it this way:
private void Awake ()
{
m_Targets[1] = GameObject.Find("Spawn1");
m_Targets[2] = GameObject.Find("Spawn2");
m_Targets[3] = GameObject.Find("Spawn3");
m_Targets[4] = GameObject.Find("Spawn4");
}
so i expected that on startup all Spawn1-4 GameObjects gets placed in the Transform Targets field but nothing happens on startup and the fileds stay empty. could you tell me why?
Are you sure you don't get any errors and the code compiles? If I try to assign a GameObject (returned by GameObject.Find()) to a Transform my code doesn't compile.
I have not tested this but shouldn't tyou put a .transform at the end of the 4 lines?:
private void Awake ()
{
m_Targets[1] = GameObject.Find("Spawn1").transform;
m_Targets[2] = GameObject.Find("Spawn2").transform;
m_Targets[3] = GameObject.Find("Spawn3").transform;
m_Targets[4] = GameObject.Find("Spawn4").transform;
}
Answer by Priyanshu · Dec 28, 2015 at 02:41 PM
You need to fill transform into Transform[] array as @12boulla suggested. Since GameObject.Find returns GameObject type.
m_Targets[0] = GameObject.Find("Spawn1").transform;
You also need to specify array size inside Awake()
m_Targets = new Transform[number of items]; // In your case 4 m_Targets[0] = GameObject.Find("Spawn1").transform;
Array cells start from 0 instead of 1. If u start with m_Targets[1], u will waste 0th cell.
m_Targets[0] = GameObject.Find("Spawn1").transform;
If you don't have prior knowledge of, how many objects u want to save. You might need List.
Hey! Thanks! The lists are for when i dont know the size of the [number of items] right?
Yes. And arrays are faster than List. So always try to give them precedence. Gud Luck :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612054115im_/https://answers.unity.com/themes/thub/images/avi.jpg)