I have three child game objects(as you can see in below picture with Red, Pink & Blue). They are child of parent game object Green.
I don’t know, how to calculate size of Parent(Green) GameObject?
I am creating all these game objects at runtime using this code:
GameObject CreatePattern(int difficultyLevel)
{
GameObject gameObjectTemp = new GameObject();
SinglePattern singlePattern;
gameObjectTemp = (GameObject) Instantiate(gameObjectTemp);
singlePattern = freeRunDataHandler.GetSinglePatternRandom(1);
GameObject gameObjectSingleObject = null;
foreach (SingleObject singleObject in singlePattern.singleObjectList)
{
gameObjectSingleObject = GetGameObjectByCategory(singleObject.catergory, singleObject.type);
if (gameObjectSingleObject != null)
{
gameObjectSingleObject = (GameObject) Instantiate(gameObjectSingleObject, new Vector3(singleObject.positionX, singleObject.positionY, singleObject.positionZ), Quaternion.identity);
gameObjectSingleObject.transform.localScale = new Vector3(singleObject.length, 1, singleObject.width);
gameObjectSingleObject.transform.parent = gameObjectTemp.transform;
}
}
return gameObjectTemp;
}
This function returns parent(Green) gameObject after adding all childs. My Parent(Green) have nothing attached to it not even any component(BoxCollider, MeshFilter, MeshRenderer, etc..).
I had attached BoxCollider, MeshRenderer & MeshFilter(Just for testing) & i tried on parent:
parent.collider.bounds.size.x ----- > box collider
parent.renderer.bounds.size.x ----- > mesh renderer
But nothing works. There return 1 or zero in cases.
Please help me in how to get size of Parent(Green) GameObject?

By size do you mean the bounds? If so, it should be a lot simpler than you’ve made it. My code is untested, I don’t have Unity handy but this should work. It assumes ‘parent’ is a game object in your hierarchy and that it has ‘children’ under it in the hierarchy.
Update:
Alternatively, if you don’t want a parent with a renderer:
You mentioned not wanting to use a parent/child hierarchy. If you don’t go that route you need to either add the “children” to some sort of array or you’ll have to use the GameObject.Find() method to find each child by name. If you name something like “child_1”, “child_2” you could look them up fairly easily but it’s a hack of a lot simpler to simply create a parent object.