- Home /
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.
Answer by Mariouch · Oct 11, 2012 at 09:52 AM
Lol I actually just found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html
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!
Answer by rutter · Oct 11, 2012 at 09:20 AM
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.
The code is actually in a function call that is initiated on Start(). I used Debug.Log, and it appears that the entire array itself is null. Perhaps I'm doing something wrong here:
DoorScripts = GetComponentsInChildren(DoorScript) as DoorScript[];
Hmm... the `as` operator will return null, in the event that the typecast fails. Does your `DoorScript` class extend from `$$anonymous$$onoBehaviour`? See here for a little bit more detail: http://wiki.unity3d.com/index.php/Head_First_into_Unity_with_UnityScript#Each_.js_File_Implements_A_Class_.28By_Default.29
DoorScript is not a class, it's a script. I put "as DoorScript[]" because GetComponentsInChildren() returns a type different from the source.
Answer by Mariouch · Oct 11, 2012 at 11:42 AM
Bah, I was confused by another Unity Question. I found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html
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! :)
Answer by Mariouch · Oct 11, 2012 at 11:42 AM
I actually just found the solution here: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html
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!