- Home /
You are not allowed to call get_rigidbody when declaring a variable.
When using the speedometer script below im getting the error:
ArgumentException: You are not allowed to call get_rigidbody when declaring a variable. Move it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. NumericalSpeedometer..ctor () (at Assets/Scripts/JavaScripts/GUIScripts/NumericalSpeedometer.js:2)
How can i call the rigidbody from a the awake/start function?
var car = rigidbody; private var mph = 0; var gSkin : GUISkin;
function Awake (){
} function Update(){ var speed = car.velocity.magnitude * 2.237; mph = speed; }
function OnGUI(){
if (gSkin) GUI.skin = gSkin; else Debug.Log("StartMenuGUI: GUI Skin object missing!");
GUI.Box(new Rect(30, Screen.height - 100, 40, 70), "" + mph, "Speedometer"); }
Answer by Statement · Apr 10, 2011 at 03:39 PM
var car : Rigidbody;
//...
function Awake () { car = rigidbody; }
//...
Thanks! I had it attached to the camera and was getting an werror, but it works fine attached to the car.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121858im_/https://answers.unity.com/themes/thub/images/avi.jpg)