- Home /
Error in code
I am trying to make a ball move but even though my code is right an error is being displayd The code I am using is
#pragma strict
var rotationSpeed = 100;
function Update () {
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody.AddRelativeTorqe (Vector3.back * rotation);
}
I don't know why it is coming but the error is-
"Assets/BallControl.js(8,1): BCE0144: 'UnityEngine.Component.rigidbody' is obsolete. Property rigidbody has been deprecated. Use GetComponent() instead. (UnityUpgradable)"
and-
"Assets/BallControl.js(8,11): BCE0019: 'AddRelativeTorqe' is not a member of 'UnityEngine.Component'. "
Answer by OncaLupe · Dec 13, 2015 at 09:50 PM
As it says, 'rigidbody' call was removed from Unity. It used to be used as a quick access to an object's Rigidbody component. However, it was just a shortcut and ran a GetComponent() call in the background. They removed it because GetComponent() is slow and should not be used repeatedly. The best way to use it is storing the reference in Awake()/Start() and using the saved variable.
Also, typo in 'AddRelativeTorqe', should be 'AddRelativeTorque'.
#pragma strict
var rotationSpeed = 100;
var myRigidbody : Rigidbody;
function Start ()
{
myRigidbody = GetComponent.<Rigidbody>(); //Store a reference to this object's Rigidbody
}
function Update ()
{
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
myRigidbody.AddRelativeTorque (Vector3.back * rotation);
}
Your answer
Follow this Question
Related Questions
Is there a reason why Unity would be able to read one variable in a script and not another? 1 Answer
After exporting my project from my PC to my laptop the physics of my game work differently. 1 Answer
Left side panel in monodevelop 1 Answer
Script problem 1 Answer
How to prevent an object from instantiating inside another object? 1 Answer