- Home /
Can one specify the parent of a gameobject in an array?
Hello all. I am trying to update the parent-child relationships of some gameobjects that are contained in a GameObject[] fixed-length array (size 3). Basically, I have three simple gameobjects (no physics data, just mesh rendering, etc) loaded from Prefabs in my scene, each of which has a child GameObject that I want to assign as a child to one of three other, empty gameobjects, which act as position markers in my scene. My current approach to this is seen in "initializeVonSouffleQueue()" below.
Basically, however, I am having troubles with NullReferenceException errors, mostly coming from line 6 from the top of "updateVonSouffleQueue()" as below. I'm not quite sure how to fix things, however. At its simplest, I'm trying to do what is seen in this link, only with gameobjects contained inside arrays rather than single variables: Parenting and Re-Parenting
Any insight into how to better approach the matters of dynamic reassignment of array members and gameobject parent-child relationships in script would be helpful. Thanks.
void initializeVonSouffleQueue() {
for (int i = 0; i < souffleQueue.Length; i++)
{
souffleQueue[i] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[i].transform.position, Quaternion.identity) as GameObject;
souffleQueue[i].transform.parent = queueGUI[i].transform;
}
}
public void updateVonSouffleQueue() {
for (int i = 0; i < souffleQueue.Length-1; i++)
{
souffleQueue[i] = souffleQueue[i+1];
souffleQueue[i].transform.parent = queueGUI[i].transform;
}
souffleQueue[2] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[2].transform.position, Quaternion.identity) as GameObject;
}
[1]: http://wiki.etc.cmu.edu/unity3d/index.php/Parenting_&_Re-Parenting
Lines this long are hard to parse, I would break that into at least two different lines:
souffleQueue[i] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[i].transform.position, Quaternion.identity) as GameObject;
I'd very much like to hear your reasoning for that.
Unless you meant Arrays, as Louis said.
Why people give answers as comment... I can't understand that.
Becouse a normal answer must be a solid one. Coments are uncertain :)
Answer by skyeyemachine · Oct 27, 2012 at 08:26 AM
Hello all. I'm replying to confirm that I've since resolved my problem.
There were a few things wrong in general, and I wound up refactoring my code several times before I was done. The key points though where that 1) I wasn't keeping close enough track of what types my function calls were returning, 2) I didn't fully understand how to interact with components and GameObjects effectively in script, and 3) I was attempting to alter the parent of a prefab object directly, rather than instantiating a clone and altering that instead.
To anyone who encounters similar issues, I suggest you take a close look at exactly what types you are working with in your scripts, particularly when it comes to working with built-in arrays (not javascript Arrays). I was particularly thrown by conversions between Transforms, Objects, and GameObjects; double-check this in the Unity official documentation. Also, ensure your arrays are actually being loaded with values properly; I kept getting Null Exceptions simply because my arrays kept winding up empty for one reason or another.
Hope this helps. Thanks to all who replied.
i suggest unityGE$$anonymous$$S.com which has an outstanding beginner article on using GetComponent, and that whole aspect of of program$$anonymous$$g
regarding tracking items in a pool.
you are correct that this is an absolutely fundamental part of engineering video games. ($$anonymous$$y guess, one day Unity will include the concept as a foundation.)
Here is a novel-length chitty chat about the topic !
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
hope it helps