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?