Access function from script of all children?

Heya, I have an empty object called “AllTheDoors” that is the parent of every door in the game. Each door has a script called “DoorScript.js”, and within that, a function called recieveProperties(). How do I call this function for every child? I wrote this so far:

var DoorScripts : DoorScript[];
DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];

for (var child : DoorScript in DoorScripts)
{
    child.recieveProperties(true, true, true);
}

The only issue is that I continually receive this error:

NullReferenceException: Object reference not set to an instance of an object

Please help as soon as possible.

You might try moving some of that code into an Awake() or Start() call, like so:

var DoorScripts : DoorScript[];

function Start()
{
	DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];
	for (var child : DoorScript in DoorScripts)
	{
		child.recieveProperties(true, true, true);
	}
}

If that’s not the problem, try to figure out which reference(s) are null: the array itself, or its contents. You could, for example, add Debug.Log(child) inside the loop to list each child before operating on it.

Lol I actually just found the solution here: Unity - Scripting API: Component.GetComponentsInChildren

Instead of:
var DoorScripts : DoorScript;

I was supposed to put:
var DoorScripts : Component;

I was confused by a previous Unity Question. Bah, my bad. But thanks so much for your help anyway!

Bah, I was confused by another Unity Question. I found the solution here: Unity - Scripting API: Component.GetComponentsInChildren

Instead of:

var DoorScripts : DoorScript[];

I was supposed to put:

var DoorScripts : Component[];

Moral of the Story: Read the f-ing manual… -.-

But thanks to everyone else who helped, though! :slight_smile:

I actually just found the solution here: Unity - Scripting API: Component.GetComponentsInChildren

Instead of:

var DoorScripts : DoorScript[];

I was supposed to put:

var DoorScripts : Component[];

I was confused by a previous Unity Question. My mistake. Thanks everyone for your help, anyway!