How to get the size of the parent game object?

I have three child game objects (as you can see in the image below: Red , Pink and Blue ). They are the parents of the parent game object Green.

I do not know how to calculate the size of the parent (green) GameObject?

I create 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 the parent (green) gameObject after adding all children. My parent (green) has nothing attached to it (BoxCollider, MeshFilter, MeshRenderer, etc.).

I attached BoxCollider, MeshRenderer and MeshFilter (just for testing) and I tried the 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 on how to get the size of the parent (green) GameObject?

enter image description here

+6
source share
2 answers

By size do you mean borders? If so, it should be a lot easier than you did. My code is untested, I don't have Unity, but that should work. It assumes that the “parent” is a game object in your hierarchy and that it has “children” under it in the hierarchy.

 Bounds bounds = parent.renderer.bounds; foreach (Transform child in parent.transform) { bounds.encapsulate(child.gameObject.renderer.bounds); } 

Update:

Alternatively, if you do not want a parent with a renderer:

 // First find a center for your bounds. Vector3 center = Vector3.zero; foreach (Transform child in parent.transform) { center += child.gameObject.renderer.bounds.center; } center /= parent.transform.childCount; //center is average center of children //Now you have a center, calculate the bounds by creating a zero sized 'Bounds', Bounds bounds = new Bounds(center,Vector3.zero); foreach (Transform child in parent.transform) { bounds.encapsulate(child.gameObject.renderer.bounds); } 

You mentioned that you do not want to use the parent / child hierarchy. If you don’t follow this route, you need to either add “children” to some array, or you will have to use the GameObject.Find () method to find each child by name. If you name something like "child_1", "child_2", you can look at them quite easily, but it's just simple to create a parent object.

+24
source

This is an older post, but I had a similar need, and I ran into some problems due to several got-yas

For the first time, I noticed that my calculated parent box was wider than it should, and that it was not completely centered.

The reason was that my objects had a turn applied to them and the restrictions were never enforced; therefore, before performing my calculations, I first had to turn back to zero; then after the calculation, I was able to restore the original rotation.

In my case, not all of my child objects had renderers, in addition, my children could have children from them, so I do not want to calculate the center when transforming children. I wanted child components to display components.

Below is the code I came across; heavy comments; not optimized, but does the trick, takes into account grandchildren, if you will and process objects with the start of rotation.

  //Store our current rotation Vector3 LocalAngle = transform.localEulerAngles; //Zero the rotation before we calculate as bounds are never rotated transform.localEulerAngles = Vector3.zero; //Establish a default center location Vector3 center = Vector3.zero; //Establish a default empty bound occupiedSpace = new Bounds (Vector3.zero, Vector3.zero); //Count the children with renderers int count = 0; //We only count childrent with renderers which should be fine as the bounds center is global space foreach (Renderer render in transform.GetComponentsInChildren<Renderer>()) { center += render.bounds.center; count++; } //Return the average center assuming we have any renderers if(count > 0) center /= count; //Update the parent bound accordingly occupiedSpace.center = center; //Again for each and only after updating the center expand via encapsulate foreach (Renderer render in transform.GetComponentsInChildren<Renderer>()) { occupiedSpace.Encapsulate(render.bounds); } //In by case I want to update the parents box collider so first I need to bring things into local space occupiedSpace.center -= transform.position; boxCollider.center = occupiedSpace.center; boxCollider.size = occupiedSpace.size; //Restore our original rotation transform.localEulerAngles = LocalAngle; 
+1
source

Source: https://habr.com/ru/post/922815/


All Articles