Instantiated Child doesnt render if multiple objects share same name as parenting object?
I've been wracking my brain over this one for a while now:
I have a prefab "BasicEnemy", and a prefab "TripleBasic". 'TripleBasic' instantiates 3 'BasicEnemy', and I have a script that instantiates multiple 'TripleBasic' - all with different origin coordinates. This creates randomly placed 'squads' of enemies.
The problem I'm having is that the instantiated 'BasicEnemy' will not render - though it is created as a child of the parenting object
However, I've also tested having every child become a child of a single parent, instead of the one that instantiated it by using the following:
GameObject parent = GameObject.Find("TripleBasic(Clone)"); unit.transform.parent = parent.transform;
This creates a single parent with every child instantiated in it - and they all render perfectly fine! ...But this isn't what I want. I want three children per parent.
For extra clarity: This one creates 3 children per parent - but none render.
public GameObject unit1, unit2, unit3;
void Start()
{
GameObject unit;
Vector3 newPos;
newPos.x = transform.position.x - 0.5f;
newPos.y = transform.position.y + 0.5f;
newPos.z = 0;
unit = (GameObject)Instantiate(unit1, transform.position, Quaternion.identity);
unit.transform.parent = transform;
unit = (GameObject)Instantiate(unit2, newPos, Quaternion.identity);
unit.transform.parent = transform;
newPos.x += 1.0f;
unit = (GameObject)Instantiate(unit3, newPos, Quaternion.identity);
unit.transform.parent = transform;
}
While this one has all children under one parent, all render, but isn't the result I'm after (I need the multiple parents for something else):
public GameObject unit1, unit2, unit3;
void Start()
{
GameObject unit;
Transform unitParent =GameObject.Find("TripleBasic(Clone)").transform;
Vector3 newPos;
newPos.x = transform.position.x - 0.5f;
newPos.y = transform.position.y + 0.5f;
newPos.z = 0;
unit = (GameObject)Instantiate(unit1, transform.position, Quaternion.identity);
unit.transform.parent = unitParent;
unit = (GameObject)Instantiate(unit2, newPos, Quaternion.identity);
unit.transform.parent = unitParent;
newPos.x += 1.0f;
unit = (GameObject)Instantiate(unit3, newPos, Quaternion.identity);
unit.transform.parent = unitParent;
}
So the question is: How can I get multiple parents, which are clones of the same prefab, with three children each, that render properly?
Answer by Snorem · Oct 25, 2015 at 10:17 PM
I've been able to get the functionality that I'm after by applying a shader to the children objects, and directional lighting to the scene to show their colors properly.
However, this feel more like a hack than an actual solution. I see no reason why having all children under one parent would draw them all, while having the children divided among multiple parents would create strange results.
Edit: After further inspection, I found that my other script, which applies random colors to an object's children, was foolishly causing most scenarios to have an alpha value of 0. Problem solved. :|