The game object hierarchy is always inherent to Transform, and never to the GameObject.
From your confused question and current answer, I’m guessing you have some Transform hierarchy which looks almost exactly like this, with just 1 child:
obj_SphereContainer (parent)
- obj_Prefab (child)
Then, you could have done simply this:
obj = currentObjContainer.transform.GetChild(0); // gets the first child
But neither this or your current solution are actually good. You’re locking up the game object hierarchy through script and that can bring many head aches if you decide to change the hierarchy - specially if you don’t remember it was locked in this specific script.
You are looking for renderers inside the instantiated object. So, this is much better:
for (var rend : Renderer in currentObjContainer.GetComponentsInChildren(Renderer)) {
DoSwitch(rend);
}
Furthermore, I’d clean up your script into this:
public var prefabContainer:GameObject; // dragged the empty-gameObject (parent)
function CreateShperes()
{
var objNumber:Number;
for (var i = 0; i < objNumber; i++)
{
objNumber = Mathf.Round(Random.Range(1,4));
Instantiate(prefabContainer, transform.position, Quaternion.identity);
for (var rend : Renderer in prefabContainer.GetComponentsInChildren(Renderer))
{
switch(objNumber)
{
case 1:
rend.material = redMaterial;
break;
case 2:
rend.material = yellowMaterial;
break;
}
}
yield WaitForSeconds(0.3);
}
}