I have the following gameobject in unty:
I need to get the width and height of it from a c#
If your object has a renderer, you can use renderer. bounds. size , which will return a vector with the height and width (and depth, if it is in 3D).
Use a bounding box to calculate the height Here is an example script that will grab the total bounding box size of all child objects under the object it is attached to. Then you simply grab the y value of the bounding box size to get the overall height. // Visual display of the bounding box for testing.
There are 2 possible ways to do this. If your object has a renderer, you can use renderer.bounds.size
, which will return a vector with the height and width (and depth, if it is in 3D).
You can also get the width and height from the colliders on a gameObject as well, by using collider.bounds.size
. Try something like this:
public Vector3 size;
private MeshRenderer renderer;
private void Start() {
renderer = GetComponent<MeshRenderer>();
size = renderer.bounds.size;
}
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With