Hi guys
So i have a script on a empty that when triggered instantiates a prefab(house) that has 7 children.When I run the code below it returns the empty gameobject the prefab house object and then the children.How can I get it just to return the children of the house prefeab?
house = Resources.Load ("house", typeof(GameObject))as GameObject;
house = Instantiate (house, transform.position, transform.rotation, this.gameObject.transform);
house.name = "house";
var children = transform.gameObject.GetComponentsInChildren<Transform>();
count = 0;
foreach(var child in children)
{
count = count + 1;
up [count] = child.gameObject;
}
}
Thx
You can do this
house = Resources.Load ("house", typeof(GameObject))as GameObject;
house = Instantiate (house, transform.position, transform.rotation, this.gameObject.transform);
house.name = "house";
count = 0;
foreach(var child in house.transform)
{
count = count + 1;
up [count] = child.gameObject;
}
}
Gives me an error at line 9. Type object' does not contain a definition for
gameObject’ and no extension method gameObject' of type
object’ could be found. Are you missing an assembly reference?
I did this instead
house = Resources.Load ("cubepre", typeof(GameObject))as GameObject;
house = Instantiate (house, transform.position, transform.rotation, this.gameObject.transform);
house.name = "house";
var children = house.transform.gameObject.GetComponentsInChildren<Transform>();//put house in front of trans.pos.
count = 0;
foreach(var child in children)
{
count = count + 1;
up [count] = child.gameObject;
Debug.Log(up[count].name+count);
Which works well enough thx for the idea.
Yeah, maybe you should use Transform instead of var inside foreach, but is ok! Glad to help 