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
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.