Search Unity

Discussion Help me understand: Why does Unity not have object sizes? (Width/Height/Depth)

Discussion in 'Getting Started' started by Rejutka, Aug 11, 2022.

  1. Rejutka

    Rejutka

    Joined:
    Feb 19, 2017
    Posts:
    15
    Inside the Transform component, there is a "position" and a "scale". The latter basically applies a multiplier to the object size, whatever that is.
    The former allows you to position the object.

    But there is no way to know how big or small an object is? Not even for the built-in primitives.
    If I create, for example, a cube and want to stack a cylinder (scaled to 0.35) on top of it, I have to guess/visually check if they're really stacked upon each other.
    (I chose 0.35 because it's a random value. I know by now that a cylinder is 2 units high and a cube 1 unit. So I could calculate the position for 0.5 height.)


    Unity thrives - there's tons of great games made with it, so obviously this cannot be the huge obstacle for others that it is for me.
    Tutorials never even mention it, as if the issue didn't exist - even tutorials made for beginners.

    So where are my circuits crossed, what am I not seeing that everyone else instinctive knows, that is so obvious that no one mentions it?
    It can't be that everyone just "guesstimates" the correct positioning, or find it by trial and error. Right?
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797
    I think probably because there are many different kinds of objects and many ways to define what an object's size is.

    In the case of primitives and any other object with a Mesh, you can use Mesh.bounds to get the information I believe you're looking for:

    https://docs.unity3d.com/ScriptReference/Mesh-bounds.html
     
    Rejutka and Ryiah like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,145
    Like @flashframe mentioned you can check the size through the mesh. The reason it's not tied to the GameObject is because that's not the role of the GO for reasons including because it's possible to have more than one size. You have the visible mesh size but you can also have the size of each collider attached to the GO.

    https://docs.unity3d.com/ScriptReference/Collider-bounds.html
     
    Rejutka likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've rarely needed to know this, but since you now know that most primitives are 1 unit in each dimension (except cylinders, which are 2 units high), you just multiply that by the transform.lossyScale to get its current size.