How to get values (height, width) of a selected object in gameview?

I have a house model in my gameview. I’m using Javescript. And I need to get the height, width and depth of each object as I select them with mouse. I have a code that gives me these values, but it only works when I click objects in the design view. Can someone help me out with this? :-?

PS: This is the code that gives the values of objects when i click them in the design view.

function OnGUI (){
var thisObject = Selection.activeGameObject;

        if (!thisObject) {return;}

 

        var renderer = thisObject.renderer;

        if (!renderer) return;

 

        var bounds = renderer.bounds;

        if (bounds == null) return;

 

        var size = bounds.size;

        GUI.Label(Rect (10, 10, 200, 200), "Size\nX: " + size.x + "   Y: " + size.y + "   Z: " + size.z);

It’s simple, all you need is get clicked object - “thisObject”, but you cannot use Selection.activeGameObject at runtime, if you want to get it via mouse clicking use the Physics.Raycast (your house model have to have ColliderComponent)

Bellow code in C# (here is JS examples Unity - Scripting API: Physics.Raycast)

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.gameObject  != null)
                    {
                            var thisObject = hit.transform.gameObject;
   
                            /// your code here
                    }
            }

Hey Patico,

Thank you very much man! Couldn’t find this anywhere else. Thanks a lot!!! :slight_smile: