Get the child gameObject of a parent and not the Transform

Hello everyone!
I am stuck in a simple problem…
I have an empty-gameObject parent to a gameObject.

By script I would like to set different materials, I

public var GOContainer:GameObject; // dragged the empty-gameObject (parent)

function CreateShperes()
{
	var i : int = 0;
	var objNumber:Number;	
	var obj:GameObject;
	
	while(i < objNumber)
	{
		var currentObjContainer:GameObject;	
		currentObjContainer = GOContainer;
		objNumber = Mathf.Round(Random.Range(1,4));		
		
		Instantiate(currentObjContainer, transform.position , Quaternion.identity);

            // obj = currentObjContainer.GetChild();
	// obj is the child (only child) of currentObjContainer

		switch(objNumber)
		{
			case 1:
			obj.renderer.material = redMaterial; break ;
			case 2:
			obj.renderer.material = yellowMaterial; break ;
			case 3: 
			obj.renderer.material = greenMaterial; break ;
			case 4:
			obj.renderer.material = blueMaterial; break ;			
		}
		    		
		yield WaitForSeconds(0.3);
		
		i++;
	}
}

Well I want the child gameObject and not the transform… and the console tells me
that GameObject is not IEnumerable …
please some hints…
Thanks

I solved this way, it might not the be optimal solution, but it works like a charm:)

obj = currentObjContainer.transform.Find(“obj_Prefab”).gameObject;

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);
	}
}

Have you tried just getting the child transform and from the transform using the .gameObject parameter of the transform?

See what I think is a very similar question here…
Transform to GameObject

you need to modify this a little in case of TextMesh to gameobject this is best and work perfect for me

public TextMesh getChildGameObject(GameObject fromGameObject, string withName)
{

	Transform[] ts = fromGameObject.GetComponentsInChildren<Transform>();
	foreach (Transform t in ts) 
		if (t.gameObject.name == withName) 
	{
		TextMesh to1= t.GetComponent<TextMesh>();
		return to1;
	}
	return null;
}