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?

  • 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_Targets1, 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 1.