- Home /
'center' is not a member of 'UnityEngine.Collider'.
'center' is not a member of 'UnityEngine.Collider'.
I literally can't find a fix for this. I is basically one line of code:
distGround = collider.bounds.extents.y - collider.center.y;
What this does is get center from colldier but center...is not a member of Collider? Unity console spells collider as Collder (capital C) but they are completely different things! I think this is a recent problem since I got this script from a year ago. But anyway, do you know a fix, I tried BoxCollider.center but it didn't work for center and bounds. Thanks anyway!
Btw, this isn't a recent problem since i still have a Unity 2.6 installation and it would produce the exact same error as Unity 4.5 or 4.6
Answer by Bunny83 · Nov 13, 2014 at 06:37 PM
Well, you've mixed up things a bit ;) First of all the type of the ".collider" property is Collider and a collider doesn't have a center. Derived types like a BoxCollider / SphereCollider have a center property. Since the type of the collider property is Collider you would have to cast it into the concrete type.
However, in your case it would make more sense to use the center of the colliders bounds since you also use the extents of the bounds. So your line should look like:
distGround = collider.bounds.extents.y - collider.bounds.center.y;
I'm not sure what you want to calculate here since this calculation seems a bit strange. If you want the worldspace y position of the bottom edge of the bounding box you should do:
distGround = collider.bounds.center.y - collider.bounds.extents.y;
or simply use:
distGround = collider.bounds.min.y;
which does this calculation already for you.
Just for reference:
If you know that the collider of the object is a BoxCollider, you can simply cast it from Collider to BoxCollider:
//C#
BoxCollider boxCol = (BoxCollider)collider;
// now use boxCol.center
$$anonymous$$eep in $$anonymous$$d if the collider isn't a BoxCollider (but a Sphere-, Capsule- or $$anonymous$$eshCollider) the cast will produce a runtime error as a SphereCollider can't be casted into a BoxCollider.
To "test" which kind of collider the object has you can use two different ways (actually there are more but those are the usual, common ones):
if (collider is BoxCollider)
{
// this cast will never produce an error since we know it's a BoxCollider
BoxCollider boxCol = (BoxCollider)collider;
}
The second way is to use an "as"-cast. The as-cast will not produce an error but will simply return null if the cast fails:
BoxCollider boxCol = collider as BoxCollider;
if (boxCol != null)
{
// yes, it's a BoxCollider
}
An as cast has one thing you have to keep in $$anonymous$$d: If boxCol is null after the as-cast you can't tell if "collider" was already null (because the object doesn't have any collider) or if the cast failed because it is a different collider type.
Answer by tanoshimi · Nov 13, 2014 at 06:35 PM
center isn't a member of collider... http://docs.unity3d.com/ScriptReference/Collider.html
Did you mean collider.bounds.center
?