- Home /
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
Answer by moghes · Mar 14, 2013 at 01:41 PM
I solved this way, it might not the be optimal solution, but it works like a charm:)
obj = currentObjContainer.transform.Find("obj_Prefab").gameObject;
I know its been ages, but wouldn't it be better to go gameobject.transform.getChild(0).gameObject; the 0 here denotes the first child game object, this can be set however you please. But makes the Child and the object name independent. Just based off which object it is attached to.
@Flailer nope. It's also a bad idea, although a bit better than the idea in this answer. Both are a kind of hard coding where it isn't needed. But, please, do read my answer for more info! ;-)
@Cawas totally didn't see your post. Good answer and makes sense.
I made great use of your hard-coded method Flailer, thanks! Oddly enough it was exactly what I was looking for.
Answer by cregox · Nov 05, 2013 at 04:28 PM
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);
}
}
Answer by sradforth · Mar 14, 2013 at 12:42 PM
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
Answer by alok.kr.029.hotmail · Jun 11, 2014 at 06:56 AM
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;
}