Search Unity

Collider bounds from a prefab?

Discussion in 'Editor & General Support' started by Jim Offerman, Dec 16, 2009.

  1. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    How do I query the collider bounds from a prefab that hasn't been instanced yet? When I do:

    Code (csharp):
    1. Vector3 size = myPrefab.gameObject.collider.bounds.size
    the result is always (0, 0, 0), where I'd expect it to return the size of whatever collider is attached to my prefab.

    So... how do I get the dimensions of this prefab without making an instance first? (or is there a valid reason why I can't?)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Do you get any different result from mesh.bounds or renderer.bounds (or is the collider a different shape in any case)?
     
  3. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    It's only the collider.bounds that returns an invalid box. I cannot use the mesh or renderer bounding box, because the collider is smaller than the mesh.

    Note that collider.bounds only produces invalid results on an uninstantiated prefab. If I create an instance and query collider.bounds from that, I get exactly what I want... except that I'd like to know the collider size before creating the instance.

    Is it expected that the collider bounds would not be set properly on an uninstantiated prefab... or is this a bug?
     
  4. Molix

    Molix

    Joined:
    Apr 24, 2009
    Posts:
    92
    I would also like to know how to do this.

    I'm trying to instantiate a prefab at a position dependent on its size, but I cannot get any valid bounds information. I have tried going through the renderers and meshes in the objects, but they are not available in the prefab since they're attached as children.
     
  5. Jeff-Kesselman

    Jeff-Kesselman

    Joined:
    Apr 5, 2010
    Posts:
    99
    I am having a similar problem trying to get the word bounds of an uninstantiated prefab.
     
  6. ThalysRun0

    ThalysRun0

    Joined:
    Dec 11, 2013
    Posts:
    1
    Hello there...

    Just in case ... regarding the date issue.:p
    Depending on the Collider Type, example shows a BoxCollider
    you may use javascript DownCast like this :

    Code (csharp):
    1.  
    2. public var prefabItem:GameObject;
    3.  
    4. function getPrefabBoxCollider(tempObject:GameObject) {
    5. var tempBoxCollider:BoxCollider;
    6.  
    7.     try {
    8.         tempBoxCollider = tempObject.collider;
    9.         return tempBoxCollider;
    10.     } catch (err) {
    11.         Debug.Log('"' + tempObject.name + '", DownCast May Not Be Possible To BoxCollider On This Object.');
    12.         Debug.LogError(err.Message);
    13.         return null;
    14.     }
    15. }
    16.  
    17. function Awake() {
    18.  
    19. var tempBoxCollider:BoxCollider;
    20.     tempBoxCollider = getPrefabBoxCollider(prefabItem);
    21.    
    22.     if (tempBoxCollider != null) {
    23.         DoSomething();
    24.     }
    25.  
    26. }
    27.  
    28.  
    For efficiency typing i thought to try passing BoxCollider (collider abstract class argument :confused:) to function
    But yet, i don't know how to do this... C# maybe...
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've just hit the same snag in a sprite game. Regardless of whether I access "collider2D" or "GetComponent<BoxCollider2D>", the bounds.size always exactly Vector2.zero.

    This makes it very difficult to position things or know whether they can fit before you instantiate them.
     
  8. UNDERHILL

    UNDERHILL

    Joined:
    Jul 6, 2014
    Posts:
    13
    I am having the same problem even today with bounds reporting zero for prefabs.

    Was there ever a solution that you found?

    I really need to be able to check prefab stats during runtime.
     
  9. UNDERHILL

    UNDERHILL

    Joined:
    Jul 6, 2014
    Posts:
    13
    The design which I ended up using was to instantiate the prefab and then check for collisions with it. Since my prefabs aren't visible until they are chosen it works fine this way.
     
  10. BoazBaaz

    BoazBaaz

    Joined:
    Jan 27, 2020
    Posts:
    1
    The bounds of the collider are set after the object is created. The Collider2D/Collider has an object called Bounds. This is what you are most likely trying to excess. But this Bounds object only gets set to the values in the inspector after it is created. They are not the value of the bounds in the inspector. The inspector value is saved in a Vector2/Vector3 variable called size. So if you want to get the size of the collider before it is instantiated you use Collider.size instead of Collider.bounds. Note that the size variable is not saved in the Collider class. So if you use a BoxCollider u have to do GetComponent<BoxCollider>().size. Hope this helps anyone.

    Code (CSharp):
    1. //don't use this, this is always 0.0, 0.0, 0.0 before instantated.
    2. Collider col = GetComponent<Collider>();
    3. col.bounds.size //is a Bounds struct
    4.  
    5. //use this. (use the specific collider)
    6. BoxCollider col = GetComponent<BoxCollider>();
    7. col.size //is a Vector3 (the value assigned in the inspector)
     
    Last edited: Feb 17, 2021
  11. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    356
    Please don’t necro old posts to say thanks. This causes clutter in the forums. ;)