Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity width and height of the gameobject

Tags:

unity3d

I have the following gameobject in unty:

enter image description here

I need to get the width and height of it from a c#

like image 279
levan Avatar asked Feb 14 '19 21:02

levan


People also ask

How do you find the width of an object in unity?

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).

How do you check your height in unity?

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.


1 Answers

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!

like image 141
Eric Bishop Avatar answered Sep 21 '22 04:09

Eric Bishop