I’m trying to set up a log that prints the rpm of one of the wheel colliders in my vehicle once every frame. I have my code like this:
#pragma strict
var wheel = GetComponent("wheel_bl");
function Update () {
print(wheel.collider.WheelCollider.rpm);
}
However, Unity gives this error:
BCE0019: ‘WheelCollider’ is not a member of ‘UnityEngine.Collider’.
What am I doing wrong? The Scripting API page says that WheelCollider inherits from Collider, but it isn’t working for me.
The Error message tells you exactly what you need to know. While WheelCollider does inherit from Collider, it is not a member of Collider. When you have a Collider object, it has no inherent knowledge of the derived type’s members. The Collider must be downcast to access these members I recommend you review some of the basic OOP concepts.
Here are two solutions:
You could expose a public variable of type WheelCollider, and assign the collider to the script to access the rpm:
public class Example : MonoBehaviour {
public WheelCollider theWheelColliderOfInterest;
void YourMethod () {
Debug.Log(theWheelColliderOfInterest.rpm);
}
}
Alternatively, you can cast the GameObject’s Collider as a WheelCollider:
[RequireComponent(typeof(WheelCollider))]
public class Example : MonoBehaviour {
void YourMethod () {
WheelCollider castCollider = gameObject.collider as WheelCollider;
Debug.Log(castCollider.rpm);
}
}
I recommend the first approach.