Unity width and height of the gameobject
A GameObject
doesn't have a width or a height but the information it contains could. In your case, you have a MeshRenderer
and that conveniently has a bounds
property which will return an...
axis-aligned bounding box fully enclosing the object in world space.
If you have a reference to the MeshRenderer
on that object:
MeshRenderer renderer;
then getting the bounds is as easy as:
renderer.bounds;
and then you can observe its size
property.
Keep in mind this is the world space bounding information so all rotations and scalings will affect it. If you'd like the raw details of the mesh itself, you'll need a reference to the MeshFilter
, look at its mesh
property, and use the bounds
property on that Mesh
object.
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!