Why can't I use gameObject.collider.center in C# when I can use it in JS?

Hello there,

In JS you are able to access the colliders “center” by typing:

// JS
gameObject.collider.center = Vector3(6, 0, 9);

// C#
gameObject.collider.center = new Vector3(6, 0, 9);

However, I'm using C# and line above doesn't work.

Instead it throws this error:

error CS1061:

Type "UnityEngine.Collider" does not contain a definition for "center" and no extension method "center" of type "UnityEngine.Collider" could be found (are you missing a using directive or an assembly reference?)

How can I set the collider.center using C#?

Best Regards,

Jeppe

I removed all previous information in my answer since it just bloated the answer.

You made it clear in your comment that you use BoxCollider. Here's how you'd do it:

var collider = gameObject.collider as BoxCollider;
collider.center = new Vector3(6, 0, 9);

This also makes perfect sense why it worked in javascript! Javascript makes use of dynamic typing so you don't have to explicitly cast the type to the target type. C# is a bit more cranky when it comes to typing. C# is strongly typed and as such you can't make that shortcut that JS can do.