Collider bounds from a prefab?

How do I query the collider bounds from a prefab that hasn’t been instanced yet? When I do:

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

3 Likes

Do you get any different result from mesh.bounds or renderer.bounds (or is the collider a different shape in any case)?

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?

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.

I am having a similar problem trying to get the word bounds of an uninstantiated prefab.

Hello there…

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

public var prefabItem:GameObject;

function getPrefabBoxCollider(tempObject:GameObject) {
var tempBoxCollider:BoxCollider;

	try {
		tempBoxCollider = tempObject.collider;
		return tempBoxCollider;
	} catch (err) {
		Debug.Log('"' + tempObject.name + '", DownCast May Not Be Possible To BoxCollider On This Object.');
		Debug.LogError(err.Message);
		return null;
	}
}

function Awake() {

var tempBoxCollider:BoxCollider;
	tempBoxCollider = getPrefabBoxCollider(prefabItem);
	
	if (tempBoxCollider != null) {
		DoSomething();
	}

}

For efficiency typing i thought to try passing BoxCollider (collider abstract class argument :face_with_spiral_eyes:) to function
But yet, i don’t know how to do this… C# maybe…

I’ve just hit the same snag in a sprite game. Regardless of whether I access “collider2D” or “GetComponent”, 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.

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.

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.

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().size. Hope this helps anyone.

//don't use this, this is always 0.0, 0.0, 0.0 before instantated.
Collider col = GetComponent<Collider>();
col.bounds.size //is a Bounds struct

//use this. (use the specific collider)
BoxCollider col = GetComponent<BoxCollider>();
col.size //is a Vector3 (the value assigned in the inspector)
12 Likes

Please don’t necro old posts to say thanks. This causes clutter in the forums. :wink: