Do I need to allocate the array for GetComponentsInChildren?

I’m still learning the correct way to use arrays in Unity and I have a basic question.

I would like to get an array of all weapons attached to my vehicle. I am using this code:

// The weapons attached to the vehicle
private RangedWeapon[] m_Weapons;

void Start()
{
	// Get all weapons attached to the vehicle
	GameObject Vehicle = transform.parent ? transform.parent.gameObject : null;
	if(Vehicle)
	{
		m_Weapons = Vehicle.GetComponentsInChildren<RangedWeapon>();
	}
}

Is this sufficient? Or do I need to allocate the m_Weapons array before assigning a value to it?

Thanks for any tips!

No. The call allocates the array and returns it to you.

You don’t have to instantiate the array first. Arrays are immutable(can’t be changed, not mutable) by their very nature, so declaring and setting the array is a method is totally fine and makes sense. The empty array wouldn’t be helpful anyways.